Well the a proper solution is to test every letter of the input buffer to see whether is a vowel or not. Each time add every letter to its category and get the final results.
I think that a simple answer code is the following, think of bugs included.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *Vowls = "aeiou";
int GetVowels(const char *Buf, char **Vowels, char **NoVowels)
{
if(Buf == NULL)
{
printf("Empty Buffer.\n");
return -1;
}
else
{
//Variables.
int i;
int j = 0;
int n = 0;
//Get memory for the pointers.
*Vowels = calloc(1, sizeof(char));
*NoVowels = calloc(1, sizeof(char));
//Check for memory fault.
if(*Vowels == NULL || *NoVowels == NULL)
{
printf("Memory Error.\n");
return -2;
}
else
{
//Search each letter seperately to find what you need, save it to char arrays.
for(i = 0; i < (int)strlen(Buf); i++)
{
//Check for valid input, you can do whatever you want with it, i prefer to break as an invalid input.
if(Buf[i] == ';' || Buf[i] == ',' || Buf[i] == '!' || Buf[i] == ':')
break;
if(strchr(Vowls, Buf[i]) != NULL)
{
Vowels[0] = realloc(Vowels[0], sizeof(char) * (j+1));
*(Vowels[0]+ j) = Buf[i];
j++;
}
else
{
NoVowels[0] = realloc(NoVowels[0], sizeof(char) * (n+1));
*(NoVowels[0]+ n) = Buf[i];
n++;
}
}
if(i == strlen(Buf))
{
//Get space for the '\0'.
Vowels[0] = realloc(Vowels[0], sizeof(char) * (j+1));
NoVowels[0] = realloc(NoVowels[0], sizeof(char) * (n+1));
//Put it.
*(Vowels[0]+ j) = '\0';
*(NoVowels[0]+ n) = '\0';
return 0;
}
else
{
printf("Internal Loop Error, wrong misplaced symbol %c in Buffer in place %d.\n", Buf[i], i);
free(*Vowels);
free(*NoVowels);
*Vowels = NULL;
*NoVowels = NULL;
return -3;
}
}
}
}
int main(int argc, char *argv[])
{
char Buf[256] = "";
char *nv = NULL;
char *v = NULL;
printf("Give me the sentance:");
fgets(Buf, sizeof(Buf), stdin);
if(Buf[strlen(Buf) - 1] == '\n')
Buf[strlen(Buf) - 1] = '\0';
GetVowels(Buf, &v, &nv);
printf("NoVowels: %s\n",nv);
printf("Vowels: %s\n",v);
free(v);
free(nv);
return 0;
}
I dont remember exactly if the Vowels are these i declared but you can easily add or remove letters from the string before the function!!!
Some results:
Give me the sentance:Hi this is the new way of making code less friendly
NoVowels: H ths s th nw w f mkng cd lss frndl
Vowels: iiieeayoaioeeiey
Press any key to continue . . .
Bokarinho!