c++ - ReplaceSubstring function glitch -
i'm working on function accepts 3 c-strings , objective replace word user finds anywhere on sentence 1 he/she inputs.
below code of function:
void replacesubstring(char str1[], char str2[], char str3[]) { char *a= new char(sizeof(str1)); char *b= new char(sizeof(str1)); int len=0; for(unsigned int ind= 0; ind< sizeof(str1); ind++) { for(unsigned ind1= 0; ind1< sizeof(str2); ind1++) a[ind]= str1[ind+ ind1]; a[ind]= '\0'; if(strcmp(str1, str2)) { for(unsigned int y= 0; y< sizeof(str3); y++) { b[len]= str3[y]; len++; } ind+= (sizeof(str2)- 1); } else { cout<< "error! no match found!"<< endl; } } cout<< b<< endl; }
let show example of output:
enter string: i love mangoes
enter word want for: mangoes
enter new word replace "mangoes" with: cheese
output? chee─
can explain can improve , why causing glitch? info appreciated.
p.s: i've tried working strstr, strstr returns pointer , leaves sentence cut off , worried figuring out way skip characters end of sentence word , figuring out afterwards. how using strstr, if possible, without cutting sentence?
thank help.
you compare str1 , str2 stcmp instead of , str2 . strcmp returns 0 (= false) when strings equal. b completed chars str3, , not recopy str1.
Comments
Post a Comment