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

In C please. not C++ 1) Prompt the user for a string that contains two strings s

ID: 3799486 • Letter: I

Question

In C please. not C++

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:


(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:


(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:


(4) Using a loop, extend the program to handle multiple lines of input. Continue until the user enters q to quit. (2 pts)

Ex:

Explanation / Answer

C code:

#include <stdio.h>

int main()
{
   while(1)
   {
   char mystring[100];
   int i = 0;
   char s1[100];
   char s2[100];
   while(i<100)
   {
       mystring[i] = '';
       s2[i] = '';
       s1[i] = '';
       i++;
   }
   printf("Enter string: ");
   fgets(mystring, 100, stdin);      
   i = 0;
   int flag = 1;


   while(i<100)
   {
       if(mystring[i] == ',')
       {
           flag = 0;
           int j = 0;
           i++;
           while(mystring[i] != '')
           {
               s2[j] = mystring[i];
               i++;
               j++;
           }
           printf("First word: %s ", s1);
           printf("Second Word: %s", s2);
           break;
       }
       else
       {
           s1[i] = mystring[i];
       }
       i++;
   }
   if(flag == 1)
   {
       printf("Error: No comma in string. ");
       return 0;
   }

   }
       return 0;
}

Sample Output:

Enter string: akash, bavisar
First word: akash
Second Word: bavisar
Enter string: ajnsdsd, ssddf
First word: ajnsdsd
Second Word: ssddf
Enter string: nsnnnn ssn, jsf
First word: nsnnnn ssn
Second Word: jsf
Enter string: dsaadnd sfdfnf
Error: No comma in string.