(1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts) Ex: Enter input string: Jill Allen Error: No comma in string Enter input string: Jill, Allen (3) Extract the two words from the input string and remove any spaces. Store the strings in two separate variables and output the strings. (2 pts) Ex: Enter input string: Jill, Allen First word: Jill Second word: Allen (4) Using a loop, extend the program to handle multiple lines of input. Continue until the user enters q to quit. (2 pts) Ex: Enter input string: Jill, Allen First word: Jill Second word: Allen Enter input string: Golden , Monkey First word: Golden Second word: Monkey Enter input string: Washington,DC First word: Washington Second word: DC Enter input string: q

Respuesta :

Answer:

Check the explanation

Explanation:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#define MAX_LIMIT 50

int checkComma(char *input)

{

int flag = 0;

for(int i = 0; i < strlen(input); i++)

{

if(input[i] == ',')

{

flag = 1;

break;

}

}

return flag;

}

int main(void)

{

char input[MAX_LIMIT];

char *words[2];

char delim[] = ", ";

printf("\n");

do

{

printf("Enter input string: ");

fgets(input, MAX_LIMIT, stdin);

size_t ln = strlen(input) - 1;

if (*input && input[ln] == '\n')

input[ln] = '\0';

if(strcmp(input, "q") == 0)

{

printf("Thank you...Exiting\n\n");

exit(1);

}

else

{

if(checkComma(input) == 0)

{

printf("No comma in string.\n\n");

}

else

{

char *ptr = strtok(input, delim);

int count = 0;

while(ptr != NULL)

{

words[count++] = ptr;

ptr = strtok(NULL, delim);

}

printf("First word: %s\n", words[0]);

printf("Second word: %s\n\n", words[1]);

}

}

}while(strcmp(input, "q") != 0);

return 0;

}

Kindly check the attached image below for the output.

Ver imagen temmydbrain

Following are the program to the given question:

Program Explanation:

  • Defining a header file.
  • Defining a method "trim" that takes a string variable inside the parameter.
  • Inside the method, a two while loop is declared that checks string variable space value and the length of the string, and store its value into the string variable.
  • In the main method, a string variable "qu" is defined, which uses "getline" method to the input string value.
  • After inputting two string variables "f,s" is declared that separates the value into two parts and passes the value into the trim method, and prints its calculated value.

Program:

#include <iostream>//defining the header file

#include <cctype>//defining the header file

using namespace std;

void trim(string &s) //defining a method trim that takes string variable in parameter

{

   while(!s.empty() && isspace(s[0]))//defining a while loop that checks string variable space value  

   {

       s = s.substr(1);//holding calculated string value

   }

   while(!s.empty() && isspace(s[s.length()-1])) //defining a while loop that checks string variable length value

   {

       s = s.substr(0, s.length() - 1);//holding calculated string value

   }

}

int main() //main method

{

   string qu;//defining a string variable

   while(true) //defining a while loop that runs when it's true

   {

       cout << "Enter input string:" << endl;//print message

       getline(cin, qu);//using getline method to input value

       if(qu == "q") //defining if block that check string variable value equal to q

       {

           break;//using break keyword

       }  

       else if(qu.find(",") == std::string::npos)//using else if block that check string value contain comma  

       {

           cout << "Error: No comma in string." << endl;//print message

       }  

       else  

       {

           int i = qu.find(",");//defining an integer variable that calls find method and store its value  

           string f = qu.substr(0, i);//defining a string variable that calls substr method and store its value

           trim(f);//calling the trimSpaces method

           string s = qu.substr(i + 1);//defining a string variable that calls substr method and store its value

           trim(s);//calling the trimSpaces method

           cout <<"First word: "<<f<<endl;//print first word value

           cout <<"Second word: "<<s<<endl;//print second word value

       }

       cout << endl;

       }

}

Output:

Please find the attached file.

Learn more:

brainly.com/question/10677419

Ver imagen codiepienagoya