/////////////////////////////////////////////
#include <iostream>
#include <string.h>
using namespace std;
//prototype
char* GetVowels(char* str);
void main()
{
int stringSize = 50;
char* str = new char[stringSize];
cout << "Enter the string:";
gets(str); // this function allows to usto enter string with spaces
char* vowels = GetVowels(str);
cout << "Your vowels: "<< vowels << endl;
delete vowels;
delete str;
}
char* GetVowels(char* str)
{
int vowelsCount = 0;
int stringSize = strlen(str);
char* vowels = new char[stringSize]; //we always can be sure that vowels array isn't greater then input string
for (int i = 0; i < stringSize; i++)
{
// here we try to findthe first occurance of symbol in vowels string
// if it is there thissymbol writes to vowels array
// as strstr acceptspointers, we must to get the address from symbol
if(strstr(&str[i], "aeiouy") != NULL) // or if (strstr(&str[i],
"aeiouy"))
{
vowels[vowelsCount] = str[i];
vowelsCount++;
}
}
char* resVowels = new char[vowelsCount];
strncpy(resVowels, str, vowelsCount);
delete vowels; // if program will notwork, please, comment this line
return resVowels;
}
Comments
Leave a comment