1.

Write a function having this prototype:int replace(char *s,char c1,char c2); Have the function replace every occurrence of c1 in the string s with c2, ad have the function return the number of replacements it makes.

Answer»

int replace(char * str, char c1, char c2)

{

int count = 0;

while (*str) // while not at end of string

 if (*str == c1)

*str = c2;

count++;

} str++; // advance to next character

}

return count;

}



Discussion

No Comment Found

Related InterviewSolutions