?
Solved

Char string replace

Posted on 2003-03-20
8
Medium Priority
?
252 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
  • 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
Industry Leaders: 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

621 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