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;
    }
}
5
  • 3
    Not all compilers will properly optimize for (int i = 0; i <strlen(str); i++){. Suggest int len = strlen(str); for (int i = 0; i <len; i++){ to avoid unneeded length recalculations. Commented Jul 17, 2015 at 1: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. Commented Jul 17, 2015 at 1:18
  • 1
    Why is it considred bad practice to omit curly braces? Commented Jul 17, 2015 at 1: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? Commented Jul 17, 2015 at 3:28
  • 1
    "... for a bit" certainly is not enough. Reading this ericlippert.com/2014/03/05/how-to-debug-small-programs might help and motivate you to go for the last miles. Commented Jul 17, 2015 at 6:01

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.