question archive We must Prompt the user for a string that contains two strings separated by a comma
Subject:Computer SciencePrice:4.86 Bought11
We must Prompt the user for a string that contains two strings separated by a comma. (1 pt)
Ex:
Enter input string: Jill, Allen
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
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
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
Here's the skeleton for the code:
#include<stdio.h>
#include <string.h>
int main(void) {
/* Type your code here. */
return 0;
}

Code:
#include<stdio.h>
#include <string.h>
int main(void) {
char sentence[50];
char firstS[50]="null";
char secondS[50]="null";
int commaFound=0;
int index=0;
int index1=0;
int i=0;
while(1){
commaFound=0;
index=0;
index1=0;
i=0;
firstS[50]="null";
secondS[50]="null";
printf("Enter Input String:\n");
gets(sentence);
if(strlen(sentence)==0){
printf("Error: Please Input a Sentence\n");
}
else if(strlen(sentence)==1){
if(strcmp(sentence,"q")==0 ||strcmp(sentence,"Q")==0){
break;
}
else{
printf("Error! Need at least 2 sentences & Comma\n");
}
}
else{
for(i=0; sentence[i] != '\0';i++){
if(sentence[i]==','){
firstS[index]='\0';
commaFound=1;
continue;
}
if(commaFound==1){
secondS[index1]=sentence[i];
index1++;
}
else{
firstS[index]=sentence[i];
index++;
}
}
if(commaFound==0){
printf("Error! No Comma in String!\n");
continue;
}
else if(strcmp(secondS,"null")==0){
printf("Error! Second String Cannot be empty!\n");
continue;
}
else{
secondS[index1]='\0';
printf("First Sentence:%s\n",firstS);
printf("Second Sentence:%s\n",secondS);
}
}
}
return 0;
}
Output:
Please see the attached file for the complete solution

