How to replace vowels with @

Asked

Viewed 1,916 times

-2

I’m doing a college task that asks for a program where the user type the string [20] and I return to it the string reversed and with the vowels replaced by @s.

How do I do it?

  • 2

    Have you tried anything? What? Put the code in the question, please.

3 answers

0

C strings are arrays of characters. Use a for with an accountant i to traverse each position. In each iteration of for, you take the letter in position i and compares it to 'a', with 'e', with 'i', with 'o' and with 'u'. If one of these comparisons is true (use the ||), then you exchange the character in the string for a '@'. You will have to compare both lowercase and uppercase vowels.

Then you use another for to reverse the string, it will go through positions i from the beginning of the string to the middle, then you change the position character i with that of the position n - i - 1, where n is the string size.

0

To replace vowels with '@', one traverses character array(str), for each character checks if it is equal to a vowel (str[i] == vowels[v]), assigning the @ in the vowel that is found( str[i] = '@' )

in the example below, the result is 'st@ck@v@rfl@w' for str = "stackoverflow";

int main()
{

    char vogais[] = {'a','e','i','o','u'};
    char str[20] = "stackoverflow";
    for(int i =0; i<strlen(str); i++){
        int vogal=0;
        for(int v =0; v<5; v++){
            if(str[i] == vogais[v]) vogal= 1;
        }
        if(vogal == 1){
            str[i] = '@';
        }
    }
    printf("%s",str);
    return 0;
}

0


Well, I thought of this solution:

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

int main()
{
char vogais[] = {'a','A','e','E','i','I','o','O','u','U'};
char texto[20] = "stAckovErflow";
int i,j;

//substituindo as vogais
for(i=0; i < strlen(texto); i++)
{
    for(j=0; j < strlen(vogais); j++)
    {
        if(vogais[j] == texto[i])
        {
            texto[i] = '@';
        }
    }
}

//invertendo a String
strrev(texto);
//imprimindo no console
printf("%s\n", texto);

system("pause");
return 0;
}

I hope I’ve helped!

  • Helped a lot! Simple and understandable code, vlw

Browser other questions tagged

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