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

Enter a phrase: Happy Easter! Enter another phrase: Man without a plan is not a

ID: 3618590 • Letter: E

Question

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<conio.h> #include<string.h>
void main() { clrscr(); char *p1,*p2; char ans=null; do{ cout<<"Enter a phrase:"; gets(p1); cout<<"Enter another phrase:"; gets(p2); int l1 = strlen(p1); int l2 = strlen(p2); int w1 = cntw(p1); int w2 = cntw(p2); cout<<" Phrase ""; printph(p1); cout<<"" has<<w1<<"wordsand"<<"l1-(w1-1)"<<" characters"; cout<<" Phrase ""; printph(p2); cout<<"" has<<w2<<"wordsand"<<"l2-(w2-1)"<<" characters"; if(l1>l2) { cout<<" In C++, prhase"; printph(p1); cout<<"is greater than phrase"; printph(p2); } if(l2>l1) { cout<<" In C++, prhase"; printph(p2); cout<<"is greater than phrase"; printph(p1); } if(l1==l2) { cout<<" In C++, prhase"; printph(p1); cout<<"is equal to phrase"; printph(p2); } cout<<"Play again(y/n)?"; cin>>ans; }while(ans=='y'||ans=='Y'); getch(); }
int cntw(char *p) { int a = strlen(p); int ctr=0; for(int i=0;i<a;i++) { if(p[i]==' ') ctr++; } return ctr; cout<<" Phrase ""; printph(p2); cout<<"" has<<w2<<"wordsand"<<"l2-(w2-1)"<<" characters"; if(l1>l2) { cout<<" In C++, prhase"; printph(p1); cout<<"is greater than phrase"; printph(p2); } if(l2>l1) { cout<<" In C++, prhase"; printph(p2); cout<<"is greater than phrase"; printph(p1); } if(l1==l2) { cout<<" In C++, prhase"; printph(p1); cout<<"is equal to phrase"; printph(p2); } cout<<"Play again(y/n)?"; cin>>ans; }while(ans=='y'||ans=='Y'); getch(); }
int cntw(char *p) { int a = strlen(p); int ctr=0; for(int i=0;i<a;i++) { if(p[i]==' ') ctr++; } return ctr; if(l2>l1) { cout<<" In C++, prhase"; printph(p2); cout<<"is greater than phrase"; printph(p1); } if(l1==l2) { cout<<" In C++, prhase"; printph(p1); cout<<"is equal to phrase"; printph(p2); } cout<<"Play again(y/n)?"; cin>>ans; }while(ans=='y'||ans=='Y'); getch(); }
int cntw(char *p) { int a = strlen(p); int ctr=0; for(int i=0;i<a;i++) { if(p[i]==' ') ctr++; } return ctr; cout<<" In C++, prhase"; printph(p1); cout<<"is equal to phrase"; printph(p2); } cout<<"Play again(y/n)?"; cin>>ans; }while(ans=='y'||ans=='Y'); getch(); }
int cntw(char *p) { int a = strlen(p); int ctr=0; for(int i=0;i<a;i++) { if(p[i]==' ') ctr++; } return ctr; }
void printph(char *p) { int a = strlen(p); for(int i=0;i<p;i++) cout<<p[i]; }
Here, in order to print a character array call printphfunction. The number of words have been counted by counting the numberof space and then adding 1 to it. In both the above cases the loop runs tillstrlen(phrase) thus ensuring we just cover the characterarray. A do while loop takes care of whether the user wants to playagain.