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

Write a loop that reads c-strings from standard input where the c-string is eith

ID: 3831911 • Letter: W

Question

Write a loop that reads c-strings from standard input where the c-string is either "land", "air", or "water". The loop terminates when "xxxxx" (five x characters ) is read in. Other strings are ignored. After the loop, your code should print out 3 lines: the first consisting of the string "land:" followed by the number of "land" strings read in, the second consisting of the string "air:" followed by the number of "air" strings read in, and the third consisting of the string "water:" followed by the number of "water" strings read in. Each of these should be printed on a separate line. Assume that the maximum length of a string is 8.

Write answer in C

Explanation / Answer

#include <stdio.h>
#include <string.h>
using namespace std;

int main()
{
char s[3][8] = {"land","air","water"};
int count[3]={0,0,0};
int i;
char str[8];
printf("Enter the string: ");
scanf("%s", &str);
while(strcmp(str,"xxxxx") != 0) {
if(strcmp(str,"land") == 0) {
count[0]++;
}
else if(strcmp(str,"air") == 0) {
count[1]++;
}
else{
count[2]++;
}
printf("Enter the string: ");
scanf("%s", &str);
  

}
for(i=0;i<3;i++){
printf("%s: %d ", s[i], count[i]);
}
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter the string: land                                                                                                                                                                                                                                                 

Enter the string: land                                                                                                                                                                                                                                                 

Enter the string: air                                                                                                                                                                                                                                                  

Enter the string: water                                                                                                                                                                                                                                                

Enter the string: xxxxx                                                                                                                                                                                                                                                

land: 2                                                                                                                                                                                                                                                                

air: 1                                                                                                                                                                                                                                                                 

water: 1                                                                                                                                                                                                                                                               

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