1

I'm supposed to make a program that replaces vowels in a string for numbers, without using and using pointers.

I used the commented printf in each switch statement to debug my program, and the output when the string is: aeiou, is:

4eiou
3iou
1ou
0u
2

and at the end when printing the string it just prints a blank line, when the output should be: 43102.

I'm doing something wrong that's replacing the complete string, but I can't figure it out. Can someone help me? Thank you very much!

#include <stdio.h>

void changes(char* ptr) {

    while (*ptr != '\0') {
        switch(*ptr) {
            case 'a':
                *(ptr)='4';
                //printf("%s\n", ptr);
                break;
            case 'e':
                *(ptr)='3';
                //printf("%s\n", ptr);
                break;
            case 'i':
                *(ptr)='1';
                //printf("%s\n", ptr);
                break;
            case 'o':
                *(ptr)='0';
                //printf("%s\n", ptr);
                break;
            case 'u':
                *(ptr)='2';
                //printf("%s\n", ptr);
                break;
            default: 
            break;
        }
        ptr++;
    }

    //Print the string
    printf("%s\n", ptr);
}

int main() {

    char sString[51];
    char *charPtr = NULL;

    charPtr = &sString[0];

    printf("Introduce a string: ");
    scanf("%[^\n]s", sString);

    changes(charPtr);
}
1
  • Format code as code please. Commented Feb 3, 2018 at 4:32

1 Answer 1

1

You incrementing the pointer and printing what it points to using %s format specifier of printf. Keep a pointer to the beginning of the string and print it. You will see the desired behavior.

char *s = ptr;
while(*ptr != 0){

 ..
 printf("%s",s);
}

This will print the whole string.(And you can notice the changes that you have made).

Sign up to request clarification or add additional context in comments.

1 Comment

This makes so much sense now! Thank you very much! I was definitely needing another head to help me find my mistake :)

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.