?
Solved

Char string replace

Posted on 2003-03-20
8
Medium Priority
?
251 Views
Last Modified: 2010-04-01
I'm coding for Half-Life and I want to replace every %v in a char with the content of another char (it HAS to be %v). How can I do this?

I tried

     char lastseen[33];

     char* text = new char[100];
     string buffer2;

     sprintf(lastseen, "LGBR");

     sprintf(text, "Waht is up %v? How come I haven't seen you %v in so long?\n");
     text = strtok(text, "%v");

     while(text != NULL)
     {
          buffer2 += string( text ) + lastseen;
          text = strtok(NULL, "%v");
     }

     printf(buffer2.c_str());


but it replaced all the v's and % signs separately.
0
Comment
Question by:LGBR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 152 total points
ID: 8179273
It will happen because strtok () will break the string everywhere any of the characters specified in the second argument will occur. Means that, it will break the string wherever it comes through any of '%' or 'v' as both are specified in "%v".

Mayank.
 
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8179314
Try using strstr (). This one works:

[I have an old Turbo compiler so the 'string' class doesn't work on my system. I hope you can convert whatever is needed.]

#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>

void replace ( char * text, char * ptr, char * lastseen, int numchars )
{
     int i, j ;
     char temp[80] ;

     for ( i = 0 ; i < text - ptr ; i ++ )
          temp[i] = text[i] ; // end for

     for ( j = 0 ; lastseen[j] != '\0' ; j ++, i ++ )
          temp[i] = lastseen[j] ; // end for

     for ( ptr += numchars ; * ptr != '\0' ; ptr ++, i ++ )
          temp[i] = * ptr ; // end for

     temp[i] = '\0' ;
     strcpy ( text, temp ) ;

} // end of replace ()

void main ()
{
    char lastseen[33];

    char text[180], * ptr, * searchstring = "%v" ;
    clrscr () ;

    sprintf (lastseen, "LGBR" ) ;

    sprintf ( text, "What is up, %v? How come I haven't seen you %v in so long? " ) ;
    cout << "Initially: " << text ;
    ptr = strstr ( text, searchstring ) ;

    while(ptr != NULL)
    {
     replace ( text, ptr, lastseen, strlen ( searchstring) ) ;
     cout << "\n Intermediate: " << text ;
     ptr = strstr ( text, searchstring ) ;
    }

    cout << "\n Finally: " << text ;

} // end of main ()


Hope that helps!

Mayank.

0
 
LVL 3

Assisted Solution

by:EarthQuaker
EarthQuaker earned 148 total points
ID: 8180149
Yuck you guys are supposed to be doing C++, and to give nice-to-read code :

#include <iostream>
#include <string>

using namespace std;

int main()
{
     string strOri = "What is up, %v? How come I haven't seen you %v in so long?";
     const char *strToFind = "%v";
     const char *strToReplace= "GMBR";

     string::size_type i;
     while( (i=strOri.find(strToFind)) != string::npos )
     {
          strOri.replace(i, char_traits<char>::length(strToFind), strToReplace);
     }
     
     cout << strOri << endl;

     return 0;
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 8180175
>> Yuck you guys are supposed to be doing C++, and to give nice-to-read code :

I mentioned that:

>> [I have an old Turbo compiler so the 'string' class doesn't work on my system. I hope you can convert whatever is needed.]

Cheers :-)

Mayank.
0
 
LVL 3

Expert Comment

by:EarthQuaker
ID: 8180224
yep but anyhow LGBR must show signs of life now to tell if the solution is acceptable or if he wants a solution only with char[].

Note that mine can be turned into a func that returns a char * if needed :

const char* strReplace(string strOri, const char *strToFind, const char *strToReplace)
{
    string::size_type i;
    while( (i=strOri.find(strToFind)) != string::npos )
    {
         strOri.replace(i, char_traits<char>::length(strToFind), strToReplace);
    }
   
    return strOri.c_str();
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8180328
Ok, I didn't return a char * because I wasn't using the 'string' class, and since I was passing a char * as argument, so the changes will be reflected permanently. How about passing your strOri by reference??

Mayank.
0
 
LVL 3

Expert Comment

by:EarthQuaker
ID: 8180421
yeah sure pass it by reference, I coded this thing in 15 sec so it's not perfect there is prolly things to optimize left.

If he gonna use this function he should forget to copy immediatly what the func returns to a buffer.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9517350
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split points between mayankeagle & EarthQuaker

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question