so ive created a function that replaces all user input characters in a string with another specific user input character..the next task is to create a func that replaces a particular part of the string with a substring and ive got something down but i cant really test it becuz for some reason i cant type in the world to replace and what to replace it with..
would appreciate some help..
thnx

#include <iostream>

void strrep(char strinput[], char tobereplaced, char toreplacewith, int length);

void strrep(char strinput[], char wordtoreplace[], char wordtoreplacewith[], int length2, int length3);

using namespace std;

int main()
{
	char strinput[100], tobereplaced, toreplacewith;

	char wordtoreplace[100]; 
		 
	char wordtoreplacewith[100];

	cout<<"Enter a sentence"<<endl;

	cin.getline(strinput, 99); 

	int length = strlen(strinput);

	cout<<"Enter the character you want to replace"<<endl;

	cin>>tobereplaced;

	cout<<"Enter the character you want to replace with"<<endl;

	cin>>toreplacewith;

	strrep(strinput, tobereplaced, toreplacewith, length);

	cout<<"Enter the word from the string you want to replace"<<endl; 

	cin.getline(wordtoreplace, 99);

	cout<<"Enter the word you want to replace it with"<<endl;

	cin.getline(wordtoreplacewith, 99);

	int length2 = strlen(wordtoreplace);

	int length3 = strlen(wordtoreplacewith);

	strrep(strinput, wordtoreplace, wordtoreplacewith ,length2 ,length3);
}

void strrep(char strinput[], char tobereplaced, char toreplacewith, int length)
{ 
	char *p;

	p = strinput;

	for(int i = 0; i< length; i++, *p++)
	{
		if(*p == tobereplaced)
		{
			*p = toreplacewith;
		}
		
	}
	
	cout<<"Your string replaced with the new character is "<<strinput<<endl;

}

void strrep(char strinput[], char wordtoreplace[], char wordtoreplacewith[], int length2, int length3)
{
	char *p;

	p = strinput;

	while(*p)
	{
		for(int i2 =0, i3 = 0; i2 <length2; i2++, i3++)
		{
		
			if(*p == wordtoreplace[i2])
		{
			*p = wordtoreplacewith[i3];
		}

		*p++;
		}
		
	}


	cout<<"Your string replaced with the new character is "<<strinput<<endl;
}

Recommended Answers

All 9 Replies

After each cin>>something , add cin.ignore(80, '\n') ;

That's because "cin>>" takes what he needs (in your case character) but leaves everything else including newline char, so you have to get rid of it

After each cin>>something , add cin.ignore(80, '\n') ;

That's because "cin>>" takes what he needs (in your case character) but leaves everything else including newline char, so you have to get rid of it

im not sure i follow

ok i put that in there (still not too sure how that works)..but im kinda stuck on how i can replace a substring of the orignial string with a new substring

Here is an example of how you can use the strstr function to replace a substring with another.

If you wanted to do it in your loop. You'd have to loop through until you matched up your first string, save the pointer to the beginning of the string and then copy over the second string. If your strings were of different lengths then you might have to separate them and concatenate the pieces.

ok..but how can i corelate that to my problem..my string can have any random word be replaced..its not fixed..how would i set that up??
thnx

Here is an example of how you could do that

int main()
{
char origstring[] = "Once upon a time there were three little pigs.";
char StringToReplace[] = "were three little pigs";
char ReplaceString[] = " was a big bad wolf.";

char* ptr = strstr(origstring, StringToReplace);
if( ptr != NULL)
{
    char tmp[255] = {0};
    *ptr = 0; // truncate
    ptr += strlen(StringToReplace) + 1; // advance to beyond the string
    strcpy(tmp,origstring);
    strcat(tmp,ReplaceString);
    strcat(tmp,ptr);
    std::cout << tmp << "\n";

}


}

To clarify on stillearning's point: you will have to create a new string that will be the result of the string replacement. You have some issues to look out for - if the replacement string is larger than the target string, or smaller. Pseudo code would look something like

p = inputstr;
p3 = newstr; // You'll have to allocate space for this!
while ((p2 = strstr(p, target)) != NULL)
{
    // Copy all characters up to the match point
    strncpy(p3, p, (strlen(p) - strlen(p2)));
    // Increment the result pointer
    p3 += (strlen(p) - strlen(p2));
    // Copy the replacement string to the result
    strcpy(p3, replacestr);
    // Increment the result pointer 
    p3 += strlen(replacestr);
    // Increment the target pointer past the matched string
    p += (p2 + strlen(targetstr));
}

// handle the end-bound case
strcpy(p3, p);

All the "p" variables are char *.

Hope this helps!

ok we really havent do string func at all and i had to search around to see what exactly u did..i kinda got it but still not too clear..can u plz walk me through the code

OK,

First, you set the pointers up - one for the input string (p), and one for the output string (p3).

while ((p2 = strstr(p, target)) != NULL)

This is to get "p2" to point at the start of the string you want to replace. We do it this way so we can put it in a loop (instead of while ((p2 = strstr(inputstr, target)) != NULL) But, before you can copy the replacement string to the output string, you have to copy all of the characters from the input string that came *before* the string you want to replace. That's what this complicated-looking piece of code does:

strncpy(p3, p, (strlen(p) - strlen(p2)));

"strncpy" copies a string from (in this case) "p" to "p3", but only for a number of characters specified in the 3rd parameter. The third parm in this case is the length of the input string (p) minus the length of the string from the match position. The difference is the length of the string *before* the match position. We copy that to the output string, then push the pointer to the output string up by that same amount.
Now, we copy the replacement string to the output, and push the pointer up by the length of the replacement string:

// Copy the replacement string to the result
strcpy(p3, replacestr);
// Increment the result pointer 
p3 += strlen(replacestr);

Now, we move up the pointer in the input string to the match location (p2), plus the length of the match string:

// Increment the target pointer past the matched string
p += (p2 + strlen(targetstr));

By doing this, you can go back to the top of the while loop, and repeat.
At the end, though, you will have a chunk of string data in the input string that came after the last match. You need to copy this remaining portion to the output string:

// handle the end-bound case
strcpy(p3, p);

Here is a sample program that will illustrate the example. Note: I ran this on a Mac - not sure where you are running your code.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
        char *p, *p2, *p3;
        char inputstr[100], newstr[100], target[100], replacestr[100];
        
        strcpy(inputstr, "This is one test, one big test!"); // result: "This is a test, a big test"
        strcpy(target, "one");
        strcpy(replacestr, "a");

        p = inputstr; // Point to input string
        p3 = newstr; // Point to output string
        while ((p2 = strstr(p, target)) != NULL)
        {
                strncpy(p3, p, strlen(p) - strlen(p2));
                p3 += (strlen(p) - strlen(p2));
                strcpy(p3, replacestr);
                p3 += strlen(replacestr);
                p = (p2 + strlen(target));
        }

        // handle the end-bound case
        strcpy(p3, p);

        cout << newstr << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.