Answer to Question #215552 in C++ for Fuad Endris

Question #215552

3.Write a C++ program that accepts sentence from the user, and counts total number of vowel or consonant, and displays the vowel and consonant sentence.

4.Write a C++ program that works exactly like strcat built-in function. It accepts two strings then it concatenates the second string to the first string. Define your function.

5.Write a C++ program that works exactly like strcnmp built-in function. It accepts two strings then it compares the two strings alphabetically considering number of characters to be compared, which is specified by the user. Define your function.


1
Expert's answer
2021-07-09T05:36:03-0400
#include <iostream>
using namespace std;

int main()
{
  char line[150];
  int vowels, consonants, digits, spaces;

  vowels = consonants = digits = spaces = 0;

  cout << "Enter a line of string: ";
  cin.getline(line, 150);
  for(int i = 0; line[i]!='\0'; ++i)
  {
    if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
      line[i]=='o' || line[i]=='u' || line[i]=='A' ||
      line[i]=='E' || line[i]=='I' || line[i]=='O' ||
      line[i]=='U')
    {
      ++vowels;
    }
    else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
    {
      ++consonants;
    }
    else if(line[i]>='0' && line[i]<='9')
    {
      ++digits;
    }
    else if (line[i]==' ')
    {
      ++spaces;
    }
  }

  cout << "Vowels: " << vowels << endl;
  cout << "Consonants: " << consonants << endl;
  cout << "Digits: " << digits << endl;
  cout << "White spaces: " << spaces << endl;

  return 0;
}

Output

Enter a line of string: This is 1 hell of a book.

Vowels: 7

Consonants: 10

Digits: 1

White spaces: 6


 #include <iostream>
using namespace std;

int main()
{
    char line[150];
    int vowels, consonants, digits, spaces;

    vowels =  consonants = digits = spaces = 0;

    cout << "Enter a line of string: ";
    cin.getline(line, 150);
    for(int i = 0; line[i]!='\0'; ++i)
    {
        if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
           line[i]=='o' || line[i]=='u' || line[i]=='A' ||
           line[i]=='E' || line[i]=='I' || line[i]=='O' ||
           line[i]=='U')
        {
            ++vowels;
        }
        else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
        {
            ++consonants;
        }
        else if(line[i]>='0' && line[i]<='9')
        {
            ++digits;
        }
        else if (line[i]==' ')
        {
            ++spaces;
        }
    }

    cout << "Vowels: " << vowels << endl;
    cout << "Consonants: " << consonants << endl;
    cout << "Digits: " << digits << endl;
    cout << "White spaces: " << spaces << endl;

    return 0;
}

Output

Enter a line of string: This is 1 hell of a book.
Vowels: 7
Consonants: 10
Digits: 1
White spaces: 6



4) include <iostream>
using namespace std;

int main()
{
    string s1, s2, result;

    cout << "Enter string s1: ";
    getline (cin, s1);

    cout << "Enter string s2: ";
    getline (cin, s2);

    result = s1 + s2;

    cout << "Resultant String = "<< result;

    return 0;
}

Output

Enter string s1: C++ Programming
Enter string s2:  is awesome.
Resultant String = C++ Programming is awesome.

5) include <iostream>
using ​namespace std;

int main()
{
   ​string s1, s2, result;

   ​cout << "Enter string s1: ";
   ​getline (cin, s1);

   ​cout << "Enter string s2: ";
   ​getline (cin, s2);

   ​result = s1 + s2;

   ​cout << "Resultant String = "<< result;

   ​return 0;
}

Output

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

Fuad Endris
09.07.21, 14:48

Thanks for all...

Fuad
09.07.21, 14:42

Thanks for all....

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS