char *textmass;
//Cpp rules :)
looping through strings
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
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:
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.
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.
Okey let me rephrase myself.
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) )
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'.
Quote:Original post by superpigQuote: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
Popular Topics
Advertisement