Jump to content
  • Advertisement
Sign in to follow this  
_DaA_

looping through strings

This topic is 4231 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

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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) )

Share this post


Link to post
Share on other sites
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'.

Share this post


Link to post
Share on other sites
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 \0
char 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.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!