0
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;
    }
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Rez88
  • 57
  • 1
  • 10
  • 3
    Not all compilers will properly optimize `for (int i = 0; i – chux - Reinstate Monica Jul 17 '15 at 01:13
  • 1
    Design detail: as the length of a string is type `size_t` and that unsigned integer type may be _much_ larger than what `int` can hold, a different reporting type than `int` is needed. – chux - Reinstate Monica Jul 17 '15 at 01:18
  • 1
    [Why is it considred bad practice to omit curly braces?](http://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces?lq=1) – Barmar Jul 17 '15 at 01:30
  • You are missing includes for printf() at minimum. system("pause") is likely to have different effects on different systems, and seems unnecessary in any case. And what is your actual question? – user464502 Jul 17 '15 at 03:28
  • 1
    "*... for a bit*" certainly is not enough. Reading this http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ might help and motivate you to go for the last miles. – alk Jul 17 '15 at 06:01

1 Answers1

4

In replaceChar, anytime you call findChar(str, c1) it will always return the same value, since findChar grabs the first instance of c1 in str. So position is always the same and your loop condition is always the same.

Rather than having replaceChar call findChar, it should just loop through the string itself. Much simpler logic that way.

int replaceChar(char *str, char c1, char c2){
    int i;
    int count = 0;
    int len = strlen(str);

    for (i=0; i<len; i++) {
        if (str[i] == c1) {
            str[i] = c2;
            count++;
        }
    }
    return count;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • `findChar(str, c1)` won't find `c1` at the previous position again, because `replaceChar(str, c1, c2)` replaced it with `c2`. What happens if `c1 == c2` is a different kettle of fish, however. – Ekkehard.Horner Jul 25 '15 at 10:14