4

I am trying to make a program that will take as input a string and replace all the vowels with the * symbol. So, for "hello world", star_vowels should return "h*ll* w*rld".

What I have for code so far is:

int star_vowels(char s[]){

    int j;

    j = 0;
    while (s[j] != '0'){
        j++;
        if (s[j] = 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u'){
            putchar('*');
        } else {
            putchar(j);
        }
        return 0;
    }
}
4
  • 3
    So what exactly was your question? Commented Nov 22, 2010 at 8:13
  • 1. is this a good way of going about it Commented Nov 22, 2010 at 8:18
  • 2. if it is, what is wrong with the code that makes it so i cannot run it to do what it needs to Commented Nov 22, 2010 at 8:19
  • you are checking for zero instead of null. There is a difference between 'o' and '/0' Commented Nov 22, 2010 at 9:56

3 Answers 3

6

This code has a number of things wrong with it.

1) while (s[j] != '0') I'm fairly sure you want to be checking for the NUL character, not the character constant zero. Change '0' to '\0'

2) j++ you are incrementing j before you even look at the 0th index of your array. If you had a vowel at s[0], this would be missed. Move j++ to the very bottom of the while loop, just before the ending brace.

3) s[j] = 'a' You are using the assignment operator = here when you should be using the equality operator == instead. Using the assignment operator is legal C code and thus will compile. Unfortunately it will return true and you'll end up replacing all of your characters with asterisks

4) putchar(j); You are attempting to output 'j' (your iterator) when you really want to be outputting s[j] (your character).

5) return 0 just like in #2, your return statement is in the wrong place. You have it within the while loop when it should be outside of it. The way you have it written, the while loop will only execute the first iteration before your function goes out of scope.

int star_vowels(char s[]) {

    int j = 0;

    while (s[j] != '\0'){
        if (s[j] == 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u') {
            putchar('*');
        } else {
            putchar(s[j]);
        }
        j++;
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

4

I think your question is going to be 'everything is *' and that's because of this part of your giant if:

if (s[j] = 'a'

That will always be true. You need the ==

Also you put j++ too early - you'd skip characters because you immediately increment upon entering the loop.

Comments

4

By incrementing the j at the beginning you will lose the 0 index (the first character).

Since you want to return new data to the outside world (outside the function scope), you either allocate memory for the new data outside the function, and pass in a pointer of that data to this function, or you just allocate dynamic memory inside this function - remember to delete it.

One implementation would be:

char *star_vowels(char s[]){
    // let's allocate memory for the new string.
    // its size should be strlen(s) + 1 (the ending char).
    char *final = malloc(strlen(s) + 1); 

    int j = 0;
    while (s[j] != 0){
        if (s[j] == 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u'){
            final[j] = '*';
        } else {
            final[j] = s[j];
        }
        j++;
    }
    final[j] = 0; // end the string
    return final;
}

Working example: http://codepad.org/dd2w5cuy

1 Comment

The question asks to replace the characters - every reason to think inplace, though it's good that you've mentioned the issue and illustrated the alternative.

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.