int findChar(char * str, char c);
Searches for the character c in the string str and returns the index of the character in the string. If the character does not exist, returns -1
int replaceChar(char * str, char c1, char c2);
Searches for the character c1 in the string str and if found, replace it with c2.The function returns the number of replacements it has performed. If the character does not exist, returns 0.
int removeChar(char * str1, char * str2, char c);
Creates a copy of str1 into str2 except for the character c that should be replaced with ‘*’
Hi guys So Far I have the following Code Which is not optimal. I have been trying to debug this for a bit and finally have come here for help.
findChar(char *str, char c);
replaceChar(char *str, char c1, char c2);
int main(){
char str[] ="all";
if (findChar(str, 'l'))
printf("Character found at index: %d\n", findChar(str, 'l'));
else
printf("No Character found\n");
if (replaceChar(str, 'x', 'a') !=0){
printf("%d",replaceChar(str,'x','a'));
printf("\n");
}
else
printf("Character does not exist\n");
system("pause");
return 0;
}
int findChar(char *str, char c){
for (int i = 0; i <strlen(str); i++){
if (str[i] == c)
return i;
}
return -1;
}
int replaceChar(char *str, char c1, char c2){
int position = 0;
int count = 0;
do{
int position = findChar(str, c1);
if (position != -1){
str[position] = c2;
count++;
}
} while (findChar(str, c1) != -1);
if (count == 0){
return 0;
}
else{
return count;
}
}