Jump to content
  • Advertisement
Sign in to follow this  
JavaMava

How many character in a string in C++?

This topic is 3792 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!