How many character in a string in C++?

Started by
23 comments, last by GoDoG23 16 years, 3 months ago
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.
Advertisement
Using std::string, just use the length() member function.
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?

#include <string>int main(){    std::string s = "this is a string";    for (int i = 0; i < s.length(); ++i)    {        //...do stuff    }}
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];

"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

EDIT: Forget this

"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

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.
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.
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";

"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

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.

This topic is closed to new replies.

Advertisement