looping through strings

Started by
4 comments, last by Emmanuel Deloget 16 years, 11 months ago
char *textmass;

 

//Cpp rules :)
I have a string of text like this ^ and I what to loop through it and check every character for a certain ascii code and when I find it I want to either change the ascii code to something else or save the entire modifide string into a new string... I Hope you guys/gals understand me... I would be happy if anybody could point me in the right direction on how to solve this... /DaA
Advertisement
If you have a pointer pointing to an unknown number of characters in an array (aka a C-style string) then either you must be told the number of characters... or the string must be NULL terminated... ie you step through until you hit a 0. Alternatively you use the C string functions... or use it to initialise a string and use the C++ string functions.

Dan
Since your comment includes a reference to C++, I'm going to assume you actually want to work with C++. If that is indeed the case, you shouldn't use char* for strings.


In C++, you would use std::replace() to do what you describe:

std::string text = "Blah";std::replace(text.begin(), text.end(), 'a', 'e');std::cout << text << std::endl;


This will display the string "Bleh".

You might also want to look over the string class and its built-in functions for other similar string-manipulation tasks; there's a pretty decent amount of stuff build into the language already, which is great, because it means you don't have to do the work yourself [wink]


Also, thread moved to For Beginners.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Okey let me rephrase myself.



char text[30] = "Hello, world hello, HELLO!!!";for(int i=0;i<30;i++){    if(text == H)        text = h    else}//                                   /\// I whant to do something like that ||//to something like this ||//                       \/char* text = "Hello, world hello, HELLO!!!";



is there an easy way to loop trough it and check each letter like I would do if it was an array and using a for-loop( if(data == H) )

Quote:Original post by _DaA_
is there an easy way to loop trough it and check each letter like I would do if it was an array and using a for-loop( if(data == H) )


For starters, you can't do that on a static string like you've got there. Strings that you hard-code into your application are constant; you should not attempt to alter them in any way. If you want to do this find-and-replace operation you should make a copy of the string into a buffer and work on that.

Secondly, if you intend to be a C++ programming, do what ApochPIQ said. Char* strings are valid C++ but extremely poor style.

Thirdly, if you want to represent a char literal, enclose it with single-quotes, e.g. 'H'.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Quote:Original post by superpig
Quote:Original post by _DaA_
is there an easy way to loop trough it and check each letter like I would do if it was an array and using a for-loop( if(data == H) )


For starters, you can't do that on a static string like you've got there. Strings that you hard-code into your application are constant; you should not attempt to alter them in any way. If you want to do this find-and-replace operation you should make a copy of the string into a buffer and work on that.

Secondly, if you intend to be a C++ programming, do what ApochPIQ said. Char* strings are valid C++ but extremely poor style.

Thirdly, if you want to represent a char literal, enclose it with single-quotes, e.g. 'H'.


Well, that's a special case here: since he declares an array, this is not a static string. This is the small difference between char *str = "lit constant"; and char str[] = "lit constant"; [smile].

@_DaA_: just iterate over the string as if it was an array - because, guess what, it is an array. Just remember that a chracter constant should be enclosed in single quotes (ie 'H'). So your ode becomes:

// I assure you, you don't want to specify the length of the array. // The compiler will compute it for you, based on the string you use// to initialize this array (and it will not forget the trailing \0char text[] = "Hello, world hello, HELLO!!!";// next, we want to iterate until we find the end of the string array// (which is, conveniently, the nul character ('\0', which evaluates to 0)for (int i=0; text != '\0'; i++){  if (text == 'H') text = 'h';  // no need for a else clause. And don't forget that trailing ;  // C++ statement ends with a semi colon. }

Now, this works, but it's really neither easy nore very secure - when compared to std::string. ApochPiQ points you in the right direction.

This topic is closed to new replies.

Advertisement