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;
}