The condition
if (*p != 'a' || *p != 'e' || *p != 'i' || *p != 'o' || *p != 'u')
is wrong. For example when *p
is equal to 'e'
that is when *p is a vowel the expression *p != 'a'
yields true and this is the result of the full condition.
You have to write the if statement like
if (*p != 'a' && *p != 'e' && *p != 'i' && *p != 'o' && *p != 'u')
or like
if ( ! ( *p == 'a' || *p == 'e' || *p = 'i' || *p != 'o' || *p != 'u') )
Also following the convention for the standard C string functions the function should return pointer to the result string. And the first parameter shall have the qualifier const because the pointed string is not changed in the function.
Instead of using the long expression in the if condition you could use the standard C function strchr
to check whether the pointed character is a vowel or not.
Here is a demonstrative program that shows how the function can be defined.
#include <stdio.h>
#include <string.h>
char * filter( const char *p, char *q )
{
const char *vowels = "aeiou";
char *result = q;
do
{
if ( *p == '\0' || strchr( vowels, *p ) == NULL ) *q++ = *p;
} while ( *p++ );
return result;
}
int main(void)
{
char *str1 = "hello";
char str2[10];
puts( filter( str1, str2 ) );
return 0;
}
Its output is
hll
And you could modify the function in the way that it would skip an upper case vowels.
For example
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char * filter( const char *p, char *q )
{
const char *vowels = "aeiou";
char *result = q;
do
{
if ( *p == '\0' || strchr( vowels, tolower( ( unsigned char )*p ) ) == NULL )
{
*q++ = *p;
}
} while ( *p++ );
return result;
}
int main(void)
{
char *str1 = "HELLO";
char str2[10];
puts( filter( str1, str2 ) );
return 0;
}
The program output is
HLL