Monday, July 27, 2009

C++ - If a string is just a character array, Is there anyway to go through each character of a string?

Can I treat it like an array by putting it through a for-loop? If so can you tell me how? I am using C++.

C++ - If a string is just a character array, Is there anyway to go through each character of a string?
yes, use a for loop


char string[]="hello"


for(int i=0;i%26lt;string.strlen;i++)


cout%26lt;%26lt;string[i];





also u can use pointer arithmetic, for instance *(string+1) is the same as string[1], however the former is generally not recommended, anyways in pointer arithmetic pointer+a is really like pointer + a*sizeof(pointer type)





oh and plz answer this


http://ca.answers.yahoo.com/question/ind...
Reply:the string.strlen thing was wrong, it would be strlen(string) Report It



No comments:

Post a Comment