Write a C++ program to exercise on c-string char array andrelated library functi
ID: 3618747 • 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 “Happy Easter!” has 2 words and 13characters.
Phrase “Man without a plan is not a man.” has 8words and 32 characters.
In C++, phrase “Man without a plan is not a man.” isgreater than phrase “Happy Easter!”
Play again (y/n)? y
Enter a phrase: Window dressing
Enter another phrase: Is the bull coming back?
Phrase “Window dressing” has 2 words and 15characters.
Phrase “Is the bull coming back?” has 5 words and 24characters.
In C++, phrase “Window dressing” is greater thanphrase “Is the bull coming back?”
Play again (y/n)? n
Explanation / Answer
#include<iostream.h> #include<string.h> void main() { char phrase1[100]={0}; char phrase2[100]={0}; int count_character1=0; int count_words1=1; int a=0;int count_character2=0; int count_words2=1; int b=0;
cout<<"Enter the first phrase = "; cin.getline(phrase1,100,' '); a=strlen(phrase1);
cout<<"Enter the second phrase = "; cin.getline(phrase2,100,' '); b=strlen(phrase2); // First phrase method for(int i=0; i<=a; i++) { if(phrase1[i]!=' ') { count_character1++; continue; } else { count_words1++; } }
// Second phrase method // First phrase method for(int j=0; j<=b ; j++) { if(phrase2[j]!=' ') { count_character2++; continue; } else { count_words2++; } } cout<<" First phrase has "<<count_character1<<" "<<"and"<<count_words1<<endl; cout<<endl<<endl; cout<<" second phrase has "<<count_character2<<" "<<"and"<<count_words2<<endl; if(strcmp(phrase1,phrase2) ==0) { cout<<" It is equal "<<endl; } else { cout<<" It is not equal "<<endl; }
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.