Sign in to follow this  

How many character in a string in C++?

This topic is 3626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just started my second semester in college for programming. First day we got an assignment. Need to look through a string and do things with it, blah blah blah... My question. I can't remember how to see how many characters are in a string. I need to know this value for a for loop later on. Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Using std::string, just use the length() member function.


Sorry, my fault. I should have mentioned that my previous (first semester) was only C. I don't know how to use classes/objects/structs/ect yet. Any chance you (or any one) could show me by example?

Share this post


Link to post
Share on other sites
std::string is for C++, part of the STL (Standard Template Library).

What kind of string are you using in C? Or is it a character array, like this:


char *Text;



or


char Text[255];

Share this post


Link to post
Share on other sites
It's a character array like the second example.

Quote:
Original post by deadstar
EDIT: Forget this



Okay heres the deal. I've only started my first day in C++. We haven't learned anything yet. The professor has told us anything we used in C (last semester) will work in C++. With that he gave us our first assignment. I assume it's something thats possible knowing only C, but once we hand it in he'll show us how elements from C++ would have made it easier/more efficient.

Share this post


Link to post
Share on other sites
Quote:
Original post by JavaMava
It's a character array like the second example.


Yes? And how are strings represented as char arrays? What defines the length of a string there? There is a criteria, a value that signifies that.

Share this post


Link to post
Share on other sites
Quote:
Original post by JavaMava
It's a character array like the second example.


Then you don't 'find out' how many characters it has, you declare it yourself when you create it.

In that example, the array holds 255 characters. Or a more efficient way is to create an integer to keep track:


int NumCharacters = 100;

char Text[NumCharacters];

cout << "The char array " << Text << " has " << NumCharacters << " characters.\n";


Share this post


Link to post
Share on other sites
Quote:
Original post by JavaMava
It's a character array like the second example.


If this is a C++ programming class, change it to a std::string. Character arrays are evil beasts when they are used to represent strings. If you must use one, you can get the length of a null-terminated string with the strlen function.

Share this post


Link to post
Share on other sites
Quote:
Original post by deadstar
Quote:
Original post by JavaMava
It's a character array like the second example.


Then you don't 'find out' how many characters it has, you declare it yourself when you create it.

In that example, the array holds 255 characters. Or a more efficient way is to create an integer to keep track:

*** Source Snippet Removed ***


I see where your coming from. I guess I should give out more info about what I'm doing. I need to write a function only (not the main) that accepts a const char checks it for 1 or 0 characters, ignores all others, blah blah blah turns the binary into decimal and prints out what the ASCII characters.

In the assignment description the function should be able to handle any number of characters. I'll have a main to test it out, but the main the professor is going to use to test, wont be telling my function how many characters it's sending it. Simply sending it the const char xxxx[ ] and asking what ascii my function can pull out of it.

Share this post


Link to post
Share on other sites
Quote:
Original post by deadstar
Quote:
Original post by JavaMava
It's a character array like the second example.


Then you don't 'find out' how many characters it has, you declare it yourself when you create it.

In that example, the array holds 255 characters. Or a more efficient way is to create an integer to keep track:

*** Source Snippet Removed ***

Pity that doesn't compile.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Quote:
Original post by deadstar
Quote:
Original post by JavaMava
It's a character array like the second example.


Then you don't 'find out' how many characters it has, you declare it yourself when you create it.

In that example, the array holds 255 characters. Or a more efficient way is to create an integer to keep track:

*** Source Snippet Removed ***

Pity that doesn't compile.


What I miss?

I'm tired :P

Share this post


Link to post
Share on other sites
char * str = "Funky Monkey";
int len = strlen(str);

The variable len now stores the number of characters in the string "Funky Monkey" (12). Note that this doesn't include the NULL character, so if you need to include that it's len + 1.




Quote:

OP: but once we hand it in he'll show us how elements from C++ would have made it easier/more efficient

Quote:
...change it to a std::string.

Trying to go over his professors head and teach him the "next step" will probably only confuse him. Better to let his professor do his job.

Share this post


Link to post
Share on other sites
Quote:
Original post by JBourrie
char * str = "Funky Monkey";
int len = strlen(str);

The variable len now stores the number of characters in the string "Funky Monkey" (12). Note that this doesn't include the NULL character, so if you need to include that it's len + 1.




Quote:

OP: but once we hand it in he'll show us how elements from C++ would have made it easier/more efficient

Quote:
...change it to a std::string.

Trying to go over his professors head and teach him the "next step" will probably only confuse him. Better to let his professor do his job.


Thank you very much, this worked perfectly. Yes the other posts did confuse me.

Share this post


Link to post
Share on other sites
Quote:
Original post by deadstar
What I miss?

I'm tired :P


Arrays must have a size known at compile time. The integer variable would need to be const for it to work. Even then, it doesn't define the length of the string - only the maximum capacity.

The convention taken in C for raw char arrays is to store a '\0' character after the last character in the string. The function strlen() will start at a given char pointer and keep going until it finds such a character, returning the count of characters.

Share this post


Link to post
Share on other sites
Quote:
Original post by JBourrie
Quote:

OP: but once we hand it in he'll show us how elements from C++ would have made it easier/more efficient

Quote:
...change it to a std::string.

Trying to go over his professors head and teach him the "next step" will probably only confuse him. Better to let his professor do his job.


[embarrass] That's what I get for not reading every post carefully; I stand corrected.

Quote:
Original post by rip-off
Arrays must have a size known at compile time. The integer variable would need to be const for it to work.


To nit-pick, it would have to be static const to be known at compile time, wouldn't it?

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
The integer variable would need to be const for it to work.
But even then, would it work? I mean, I could write some nasty code that get's at the constants memory and change the value to something else at runtime.. so it still does not guarantee that the value is known at compile time. Or?

Share this post


Link to post
Share on other sites
Quote:
Original post by Driv3MeFar
Quote:
Original post by rip-off
Arrays must have a size known at compile time. The integer variable would need to be const for it to work.


To nit-pick, it would have to be static const to be known at compile time, wouldn't it?

Why would it ahve to be static? Non-static const is still constant.

Quote:
Original post by issch
Quote:
Original post by rip-off
The integer variable would need to be const for it to work.
But even then, would it work? I mean, I could write some nasty code that get's at the constants memory and change the value to something else at runtime.. so it still does not guarantee that the value is known at compile time. Or?

Changing a compile time constant, for example via a const cast of the pointer to it and changing it via a non-const pointer, is undefined behaviour. That is, anything can happen, which includes the constant not changing it's value.

Share this post


Link to post
Share on other sites
[edit: I mean compile time const. Not const int x = y ]

const is enough, AFAIK. Modifying an object declared const is undefined behaviour. This is why const_cast<> only has meaning when the original object was not const.

For example:

void frobnicate( const Foo &param )
{
Foo &foo = const_cast<Foo &>(param);
foo.modify();
}

void foo()
{
Foo foo;
frobnicate(foo); // defined

const Foo constFoo;
frobnicate(constFoo); // undefined

frobnicate(Foo()); // pure evil, but sadly only qualifies as undefined!
}




Please note: I've never used const_cast ever. I could be wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by Brother Bob
Quote:
Original post by Driv3MeFar
Quote:
Original post by rip-off
Arrays must have a size known at compile time. The integer variable would need to be const for it to work.


To nit-pick, it would have to be static const to be known at compile time, wouldn't it?

Why would it ahve to be static? Non-static const is still constant.


It may be different in C, but in C++ non-static const variables are not known at compile-time. Although you cannot change a const variable once initialized, they can still be initialized at run-time (consider const member variables in classes that are initialized with values read in from text files).

Share this post


Link to post
Share on other sites
It doesn't need to be static const. Integral constant expressions include const variables initialized with constant expressions, including literals. See section 5.19 paragraph 1 in the C++ standard.

Share this post


Link to post
Share on other sites
I stand corrected again [headshake]. I swear VS use to complain about that, but maybe I'm just loosing my mind.

Edit:
Quote:

initialized with constant expressions


I think I was missing that bit, and was thinking along the lines of King Mir's example. D'oh.

Share this post


Link to post
Share on other sites
That's a constant parameter not a constant known at compile time.

As SirCrane said const expressions are especified in 5.19 and arrays in 8.3.4

The link to a old 96 ISO C++ draft (i have nothing newer to link)

[Edited by - GoDoG23 on January 8, 2008 7:45:55 PM]

Share this post


Link to post
Share on other sites

This topic is 3626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this