A real problem!
void replace_string_with_stars(char*str)// i must replace the string with 5 stars
{
char arr[60];
strcpy(arr,str);//lets assume that str is (aaahftgaaahftdsgaaa)
cout<<"enter astring"<<endl;
while(cin.get()!='\n')
continue;
char s2[20];//s2 would be aaa
cin.getline(s2,20);
int c=strlen(s2);
char *q=strstr(arr,s2);// im searching for string 2 in the array
while(q!=NULL){
if (c<5){// if string length is less than 5
for(int i=strlen(arr);i>arr[q+(c-1)];i--)// i just need away to let the pointer tell me its position in the array
arr[i+(5-c)]=arr;
for(int j=q;j<6;j++)
arr[j]='*';
}
else
if(c>5){
for(int i=q+4;i<strlen(arr);i++)
arr=arr[i+(c-5)];
for(int j=q;j<6;j++)
arr[j]='*';
}
else
if(c=5){
for(int j=q;j<6;j++)
arr[j]='*';
}
q=strstr(arr,s2);
}
cout<<arr<<endl;
}
any ideas will be helpfull.
Any ideas? You haven't told us the problem. As for ideas, post your code inside the
or [ source ] tags (without spaces). Also you are using C++ so there is no reason to use char arrays/pointers for strings.
#include <iostream>#include <string>using namespace std;void replace_string_with_stars(string& s1){ string s2; getline(cin,s2,'\n'); basic_string <char>::size_type pos,off=0; pos=s1.find(s2,off); while(pos!=string::npos) { off=pos+5;//We do not want to get an infinite loop if someone tries to replace “*" with “*****". s1.replace(pos,s2.length(),"*****"); pos=s1.find(s2,off); } cout<<s1<<endl;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement