Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to exercise on c-string char array andrelated library functi

ID: 3643477 • Letter: W

Question

Write a C++ program to exercise on c-string char array andrelated library functions. Make two char arrays each can hold asmany as 80 characters. Ask user to input two phases and store themin these two arrays. The program will then analyze and comparethese two phases and display whether these two phrases are equal;if not, which one is greater; how many characters are on eachphrase, and how many words are on each phrase. Needs to be writtenin Visual Studio 2008 format with # include <iostream> Belowis a sample run:

Enter a phrase: Happy Easter!

Enter another phrase: Man without a plan is not a man.

Phrase

Explanation / Answer

//Here ya go

// Don't forget to rate :)

//-------------------------------

#include <iostream>

using namespace std;

int GetNumWords (char *); // Counts the number of words

void Compare (char *, char *); // Compares the two and prints the result


int main()
{
char Phrase1 [81]; // Maximum 80 chars + 1 for end of string
char Phrase2 [81];

char Continue = 0; // This is for the loop over the whole program


do {
cout << "Enter phrase: ";
cin.getline (Phrase1, 81); // Reads a maximum of 81 chars stops at ' '

cout << "Enter another phrase: ";
cin.getline (Phrase2, 81);

cout << "Phrase: " << Phrase1 << ", has " << GetNumWords(Phrase1) << " words and " << strlen(Phrase1) << " characters." << endl; // strlen gives the total number characters

cout << "Phrase: " << Phrase2 << ", has " << GetNumWords(Phrase2) << " words and " << strlen(Phrase2) << " characters." << endl;

Compare (Phrase1, Phrase2);

cout << "Play Again (y/n)? ";
cin >> Continue;
cin.ignore (); // This is necessary when using a combination of getline and cin
cout << " ";
    } while ((Continue == 'y') || (Continue == 'Y'));

}

int GetNumWords (char * str)
{
   bool inSpaces = true;
   int numWords = 0;

   for (int i = 0; str [i] != ''; i++) // Goes until it reaches end of string
   {
      if (str [i] == ' ')
      {
         inSpaces = true;
      }
      else if (inSpaces) // Basically was last character a space if so then start of a new word
      {
         numWords++;
         inSpaces = false;
      }
   }

   return numWords;
}

void Compare (char * str1, char * str2)
    {
    int cmp = strcmp (str1, str2);
    if (cmp == 0)
        {
        cout << "In C++, the phrase: " << str1 << ", equals the phrase: " << str2 << endl;
        }
    else
        {
        if (cmp > 0)
            {
            cout << "In C++, the phrase: " << str1 << ", is greater than the phrase: " << str2 << endl;
            }
        else
            {
            cout << "In C++, the phrase: " << str1 << ", is less than the phrase: " << str2 << endl;
            }
        }
    }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote