(C) Make an algorithm that reads a string and removes vowels and whitespace

0

The code is copying ALL characters from one string to another and ignoring the copy condition. I know there is an easier way to remove spaces and vowels, which is bringing the next character to the current position, but the code was asked to do so.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Faça um algoritmo que leia uma string e remova as vogais e os espaços em 
branco. */

int main(int argc, char *argv[]) {
system ("color 0a");
char str[1000], newstr[1000];
int i = 0;
printf ("Insira uma string:\n");
gets(str); 
while (str[i] != '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Faça um algoritmo que leia uma string e remova as vogais e os espaços em 
branco. */

int main(int argc, char *argv[]) {
system ("color 0a");
char str[1000], newstr[1000];
int i = 0;
printf ("Insira uma string:\n");
gets(str); 
while (str[i] != '%pre%'){
    if (str[i] != 'a'||str[i] != 'e'||str[i] != 'i'||str[i] != 'o'||str[i] 
!= 'u'){
        newstr[i] = str [i];
    }else{
            if (str[i] != 'A'|| str[i] != 'E'|| str[i] != 'I'|| str[i] != 
'O'|| str[i] != 'U'){
                newstr[i] = str [i];
        }else{
                    if (str[i] != ' '){
                        newstr[i] = str [i];
            }
        }       
    }
    i++;
}
printf ("String sem vogais:\n");
puts (newstr);
system ("pause");
return 0;
}
'){ if (str[i] != 'a'||str[i] != 'e'||str[i] != 'i'||str[i] != 'o'||str[i] != 'u'){ newstr[i] = str [i]; }else{ if (str[i] != 'A'|| str[i] != 'E'|| str[i] != 'I'|| str[i] != 'O'|| str[i] != 'U'){ newstr[i] = str [i]; }else{ if (str[i] != ' '){ newstr[i] = str [i]; } } } i++; } printf ("String sem vogais:\n"); puts (newstr); system ("pause"); return 0; }
    
asked by anonymous 14.09.2018 / 20:13

1 answer

1
 int i = 0, x=0;
    printf ("Insira uma string:\n");
    fgets(str, 999, stdin);     
    while (str[i] != '
 int i = 0, x=0;
    printf ("Insira uma string:\n");
    fgets(str, 999, stdin);     
    while (str[i] != '%pre%')
    {
        if (str[i] == 'a'||str[i] == 'e'||str[i] == 'i'||str[i] == 'o'||str[i]== 'u' || str[i] == 'A'|| str[i] == 'E'|| str[i] == 'I'|| str[i] =='O'|| str[i] == 'U')
        {
            i++;
            continue;
        }
        else
        {
            if (str[i] != ' ')
            {
                newstr[x++] = str [i];
            }
        }
        i++;
    }
') { if (str[i] == 'a'||str[i] == 'e'||str[i] == 'i'||str[i] == 'o'||str[i]== 'u' || str[i] == 'A'|| str[i] == 'E'|| str[i] == 'I'|| str[i] =='O'|| str[i] == 'U') { i++; continue; } else { if (str[i] != ' ') { newstr[x++] = str [i]; } } i++; }

I made these small changes, put everything in a line of if , if it is the same then it does nothing, continue .

Another important step is to use newstr[x++] because doing newstr[i] . Ex: fabio

What you will do will be put in the 0 f position and then put in the 2 b ...

That is, newstr would get something like f_b and in that space can be any character.

  • Never gets(str); , change fgets(str, 999, stdin); , because with gets if the user enters a string greater than 1000 it breaks the program.

Code on Ideone

    
14.09.2018 / 20:52