Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

penetrator

Replace string chars

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

Advertisement
are you using stl strings or char*

dunno about std::strings but for char* such a function doesnt exist, but its trivial to write one yourself:


        
char kill = 'a';
char replace = 'z';
for ( int i = 0; i < strlen(str); i++ )
if ( str[i] == kill )
str[i] = replace;


this will replace all 'a' with 'z' when str is either char* or std::string when you replace strlen(str) with str.length()



Runicsoft -- latest attraction: obfuscated Brainfuck Interpreter in SML

This post was made entirely from re-cycled electrons


[edited by - Burning_Ice on January 19, 2003 9:10:55 AM]

Share this post


Link to post
Share on other sites
Hey, Your .sig is too big!

Now...
quote:
Original post by penetrator
and if i should replace, for example, "......" with "..", what the code would be ?


In that case you're creating a new string (".." is shorter than "......"), so it's not just a matter of replacement.

    
char * found = 0;
char formed[60] = {0};
const char text[60] = "Some very long string incorporating a...... triple ellipsis";
const char * trellipsis = "......";
 
found = strstr( text, trellipsis );
if( found != 0 && found != text )
{
strncpy( formed, text, (found - text) );
strcat( formed, ".." );
strcat( formed, (found + 6) );
}

And that is why you should use std::string.

[edited by - Oluseyi on January 19, 2003 11:50:33 AM]

Share this post


Link to post
Share on other sites
Still not working. All i want to do is to store string values which are separated by spaces, sometimes 2, sometimes 4 or 5 spaces, into 1 array. So just imagine the string is smt like this:

char mystring[8192]="1 4 6 7 2";

i want to store values into a simple array, so that i get smt like:

myarray[1]=1;
myarray[2]=4;
myarray[3]=6;
myarray[4]=7;
myarray[5]=2;

I was used to to this in VB very easily with a Split function, but in VC++ i''m lost !

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
#include <sstream>

int main() {
char mystring[8192]="1 4 6 7 2";
std::stringstream s;
s << mystring;
int a[5];
for (int i=0; i<5; ++i)
s >> a;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

  
#include <sstream>

int main() {
char mystring[8192]="1 4 6 7 2";
std::stringstream s;
s << mystring;
int a[5];
for (int i=0; i<5; ++i)
s >> a[i];
}

Share this post


Link to post
Share on other sites
quote:
Original post by penetrator
Still not working. All i want to do is to store string values which are separated by spaces, sometimes 2, sometimes 4 or 5 spaces, into 1 array...

In the future, say what you want to do. It saves both us and you time explaining stuff that doesn''t solve your problem.

Share this post


Link to post
Share on other sites

  • 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!