Exchange vowels for "*"

Asked

Viewed 44 times

-2

I’m making a program to replace the vowels of a typed phrase with * and show to user:

#include<stdio.h>
#include<string.h>
#define TAM 50

int main(){
    char f[TAM];
    int t_f,n;
    printf("Digite a frase:\n");
    fgets(f,TAM,stdin);
    t_f=strlen(f);
    for(n=0;n<=t_f;n++){
        if((f[n]=='A') ||(f[n]=='a')|| (f[n]=='E')|| (f[n]=='e') || (f[n]=='I') || (f[n]=='i') || (f[n]=='O') || (f[n]=='o') || (f[n]=='U') || (f[n]=='u')){
            f[n] ='*';
        }
    }
    for(n=0;n<=t_f;n++){
        printf("Frase criptografada:%s",f[n]);
    }
    return(0);
}

I don’t understand why the program doesn’t run and the message "Segmentation fault (core dumped)" is showing.

  • Here at br OS have dozens of answers to this problem, take a look here www.shorturl.at/flPU3

  • This helps you: https://answall.com/questions/52555/erro-segmentation-fault-core-dumped

  • 1

    Why do you make one for traversing n to display the final sentence?

  • Note that in C an array (which can be a string: a character array followed by the terminator character ' 0') the indices range from 0 to size-1. Your command for considers one more position. This is an error but not the cause of the reported problem that is in the last printf.

1 answer

1

That piece of code doesn’t make sense:

for(n=0;n<=t_f;n++){
    printf("Frase criptografada:%s",f[n]);
}

You’re going through the entire sentence to display character to character, but you put as printf the text "phrase", indicating that it wants to display the entire sentence, adds %s to display a character chair, but passes as a parameter of printf the value f[n] which is a character. In half of the code you give clues that you want to display character by character and in the other half you give clues that you want to display the entire sentence. In the end, he mixed it all up.

To display the result, just do:

printf("Frase criptografada: %s", f);

For f is already a string.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.