Remove vowels in a line. What am I doing wrong?
I am trying to remove all vowel strings by putting all consonants in another array and then reinitializing the first array to the second. After that, it is assumed that the first array will be printed, i.e. only consonants. I'm really not sure where the problem is, I've been looking at this issue for three hours and I'm so tired of it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str [100];
char garbage[100];
int loopcondition = 0 , j = 0 , k = 0;
int userinput;
char a = "a" , e = "e" , i = "i", o = "o" , u = "u";
printf("!!!Hello World!!! Please type in a string from which I can remove all the vowels:\n");
//This program is supposed to take in a string and then delete all the vowels
gets(str); //gets user input
userinput = strlen(str); //makes userinput equal to the length of the string
while(loopcondition < userinput) //the loop runs until the condition is no longer smaller than the input
{
loopcondition++; //incrementation of condition
if(str[j] != a || e || i || o || u) // this should check wether the current position in the array is a vowel
{
garbage[k] = str[j] ; //if it not the it puts the current character into the garbage array
k++; //iteration of the garbage array position
};
j++; //incrementation of string array position
};
garbage[k] = '\0';
strcpy (str , garbage ); //this copies the string?!?! aka reinitiliazing array variable
printf("%s" , str);
return 0;
}
source to share
Initialization char
should look like
char a = 'a' , e = 'e' , i = 'i', o = 'o' , u = 'u';
Remember that double quotes ( " "
) are for string literals, single quotes ( ''
) are for character literals.
Then
if(str[j] != a || e || i || o || u)
this is not how you use logical OR ( ||
) operators in c. The chain is impossible. You must check each condition separately. Something like
if( (str[j] != a) &&
(str[j] != e) &&
(str[j] != i).......//so on
However, in my opinion, if you change your logic to use case switch
it will be a better design.
Oh, and it is better to use the int main(void)
recommended one in the standards.
source to share
char str [100];
char *src; // source position of copying characters
char *dst; // destination position
...........
gets(str); //gets user input
printf( "Input: %s\n" , str);
for( dst = src = str; *src; src++ ) // scan chars of the string
{
if( *src != 'a' && *src != 'e' && // check if a vowel
*src != 'i' && *src != 'o' && *src != 'u')
{
* dst ++ = * src; // no? copy it
};
}
*dst = 0; // ASCII NUL (zero) char to terminate the string
printf( "Result: %s\n" , str);
The loop for
checks the entire string with a pointer src
, from start ( str
) to end (null character, ASCII NUL detected by expression *src
, equivalent *src != 0
).
Each character is checked, and if it is not a vowel, it is copied to a position dest
, which is then advanced by one ( * dst ++ = * src
). Initially, the variable is dest
set to str
, so all characters are copied one by one at the beginning of the string, except for vowels. The last assignment ( *dst = 0
) ends the line.
source to share
You are going 1 char at a time to check if it's a vowel, so instead of using (unsafe) gets()
or using multiple arrays at all, you can just use getchar()
and only add non-vowels to your array. This simplifies the rest of the logic, so you can just use pointers.
#include <stdio.h> //for puts and getchar
int main(void){
char s[128], *sp = s, *end = sp + sizeof(s) - 1;
puts("Please type in a string");
while ((*sp=getchar())!='\n' && sp<end){
switch(*sp|32){ // |32 will catch uppercase vowels too
case 'a': case 'e' : case 'i' : case 'o' : case 'u' :
continue; //*sp will be overwritten
default :
sp++;
}
}
*sp=0;
puts(s);
}
or using strch()
#include <stdio.h> //for puts and getchar
#include <string.h> //for strchr
int main(void){
char s[128], *sp = s, *end = sp + sizeof(s) - 1;
puts("Please type in a string");
while ((*sp=getchar())!='\n' && sp<end)
if (!strchr("aeiouAEIOU",*sp)) sp++;
*sp=0;
puts(s);
}
source to share