How many character in a string in C++?

Started by
23 comments, last by GoDoG23 16 years, 3 months ago
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.

Advertisement
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.
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

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

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.

Check out my new game Smash and Dash at:

http://www.smashanddashgame.com/

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.

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.
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?
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?

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.
[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.

This topic is closed to new replies.

Advertisement