3

My function gets a char array as input. If it includes character e it changes it with a and returns the new char array. Here is my code:

char echanger(char word[]){

    int total = 0;
    int i;
    char final[5];

    for(i=0;i<5;i++){
        if(word[i]=='e'){
            final[i] == 'a';
        }
        else{
            final[i] == word[i];
        }
    }

    return final;
}

I call it in in main() function like this:

int main(){

    char a[] = "helle";
    printf("new string is: %d \n",echanger(a));

}

it gives me this output:

new string is: -48

what I'm missing in here?

3
  • 1
    I assume final[i] == 'a'; must be final[i] = 'a'; (ASSIGN not EVAL) Commented Mar 17, 2015 at 20:23
  • 1
    Your function is declared to return char but you're returning a char array. Commented Mar 17, 2015 at 20:23
  • Also you're trying to return a local variable, that gets destroyed by the end of the function. Commented Mar 17, 2015 at 20:32

3 Answers 3

3

Your program has several errors. Assuming the string length. Not allowing for, or writing , the nul terminator. Not returning the pointer. Using == where you mean =. Trying to return a local variable.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *echanger(char *word) {
    int len = strlen(word);
    int i;
    char *final = malloc(len+1);
    for(i=0; i<=len; i++) {         // include terminator
        if(word[i] == 'e')
            final[i] = 'a';
        else
            final[i] = word[i];
    }
    return final;
}

int main(){
    char a[] = "helle";
    char *news;
    news = echanger(a);
    printf("new string is: %s\n", news);
    free(news);                     // was from malloc
    return 0;
}

Program output:

new string is: halla
Sign up to request clarification or add additional context in comments.

Comments

1

You are using equality (==) operator instead of for assignment (=) operator. Change final[i] == 'a' to final[i] = 'a'.

If I rewrite the function with the changes then it will be -

char echanger(char word[]){

    int total = 0;
    int i;
    char final[5];

    for(i=0;i<5;i++){
        if(word[i]=='e'){
            final[i] = 'a';
        }
        else{
            final[i] = word[i];
        }
    }

    return final;
}

Hope it will help.
Thanks a lot.

3 Comments

yes that's one problem. i think the other is i call it with %d I think i should use something else
Oh sure! In main method you can use %s.
This still doesn't work. You're returning a local variable. The return type is char but you're returning char[5]. I have no idea why that even compiles in C.
1

Why not just modify the original string instead of creating a new one?

void echanger(char word[]){

    int i;
    int n = strlen(word);

    for(i=0;i<n;i++){
        if(word[i]=='e'){
            word[i] = 'a';
        }
    }
}

Note: = is assignment. == is comparison.

Also you need to use the %s format specifier in printf to output strings. %d will try to read the string as an int which gives you the weird value.

Also FWIW char final[5] is not large enough to hold the C-string "helle". Remember the terminating null character.

3 Comments

Note that my version returns void. You should now have something like char a[] = "helle"; echanger(a); printf("%s\n", a);
but i don't want to make it void?
Then make it return char* and allocate a new char array with malloc. That's the only way. However you would have a memory leak if you'd then do printf("%s\n", echanger(a)); because you couldn't free the memory echanger allocated. Making it void and just modifying the parameter would be the safest way to do it. See Weather Vane's answer for more details.

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.