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.
Expert's answer
#include <iostream>
#include <string.h>
using namespace std;
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],
           vowels[vowelsCount] = str[i];
    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!


No comments. Be the first!

Leave a comment

Ask Your question

New on Blog