Answer to Question #49773 in C++ for Saad Abdollah Motamed

Question #49773
Write C++ program that reads the string and prints the vowels and their number. The program takes a string from the user and stores it in a character array and store all its vowels in another array of type char and counts the number of vowels found.
1
Expert's answer
2015-06-05T04:06:13-0400
/////////////////////////////////////////////
#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;
}



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS