Vowel count

Asked

Viewed 209 times

2

I need to make a code that takes a string typed by the user, count how many vowels there are in that string and swap all vowels from that string to a letter that the user defines, I did almost everything, I just can’t replace all the vowels of the word by the letter the user type, which error?

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

int main()
{
    char string[100];
    char ch1,ch2,texto[100];
    int x, tam,i,j,contador=0;
    char vogais[] = "aeiouAEIOU";
    
    printf ("Digite uma palavra: ");
    gets(texto);
 
    for (i=0;i<strlen(texto);i++){
        for (j=0;j<strlen(vogais);j++){
                if (texto[i] == vogais[j]){
                        contador++;
                }
        }
    }
   
    if (contador == 1){
        printf ("\n\nA palavra informada possui 1 vogal\n\n");
        }else{
                printf ("\n\nA palavra informada possui %d vogais\n\n", contador);
        }
    printf("Digite a palavra novamente :");
    gets(string);
    printf ("Qual letra voce vai substituir? :\n");
    scanf ("%c", &ch1);
    printf ("Letra que vai substituir :\n");
    scanf (" %c", &ch2);
    tam=strlen(string);
    for (x=0;x<tam;x++){
        if (string[x]==ch1){
            string[x]=ch2;
        }
    }
    printf ("%s", string);
    return 0;
    
}

2 answers

1

It makes no sense to ask the person to type twice the word (who guarantees that the same word will be typed in both times?). Read the word only once.

And if the idea is to exchange all vowels for a specific letter, it doesn’t make sense to read the ch1. Read only what will be changed and ready.

Another detail is that you don’t need to create an array of all vowels and make one for in it every time. Just test each word character and check if it is a vowel.

Another point is that when using strlen all the time, you are creating an inefficient algorithm <- read this link to better understand, but basically you are using the so-called Shlemiel the Painter’s Algorithm (summing up, strlen traverses the entire string to determine its size, and you are calling it several times within a loop, which is very inefficient).

Not to mention the very idea of this loop in vogais is inefficient in itself. For example, if you have seen that the letter is a, so that continue the loop on the other vowels? Anyway, both this loop how much the vowel array is unnecessary, just do so:

int qtdVogais = 0;
char troca, palavra[100];

printf("Digite uma palavra: ");
fgets(palavra, 100, stdin);
printf("\nAs vogais serão substituídas por qual letra?\n");
scanf("%c", &troca);

for (int i = 0; palavra[i] != '\0'; i++){
    switch(palavra[i]) { // se é vogal, atualiza o contador e troca a letra
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
            qtdVogais++;
            palavra[i] = troca;
    }
}

if (qtdVogais == 1)
    printf("\nA palavra informada possui 1 vogal!\n");
else
    printf("\nA palavra informada possui %d vogais!\n", qtdVogais);

printf("Resultado: %s\n", palavra);

0

His main mistake was not having checked the vowels after reading the letter that would be replaced, example:

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

#define MAX 100

int main()
{
    setlocale(LC_ALL, "Portuguese"); // Define a acentuação como UTF-8

    int total = 0;
    char x, string[MAX], vogais[] = "aeiouAEIOU";

    printf("Digite uma palavra: ");
    fgets(string, MAX, stdin); // É recomendado utilizar a função "fgets" do que a função "gets" para não ocorrer Buffer Overflow

    setbuf(stdin, NULL); // Limpa o buffer

    printf("\nAs vogais serão substituídas por qual letra?\n");
    scanf("%c", &x);

    for(int i = 0; i < strlen(string); i++){
        for(int j = 0; j < strlen(vogais); j++){
            if(string[i] == vogais[j]){
                string[i] = x;
                total++;
            }
        }
    }

    if(total == 1)
        printf("\nA palavra informada possui 1 vogal!\n");
    else
        printf("\nA palavra informada possui %d vogais!\n", total);
    
    printf("Resultado: %s\n", string);
    return 0;
}

Browser other questions tagged

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