Question

In: Computer Science

In C Please *Asked before but all previous answers do not work correctly* 10.6 LAB: Warm...

In C Please

*Asked before but all previous answers do not work correctly*

10.6 LAB: Warm up: Parsing strings

(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

Solutions

Expert Solution

Code

#include <stdio.h>

#include<string.h>

int checkComma(char []);

#define MAX_SIZE 50

int main() {

char str[MAX_SIZE];

char firstWord[MAX_SIZE/2];

char secondWord[MAX_SIZE/2];

int commaPos,i,j;

while(true)

{

printf("Enter input string:\n");

scanf("%[^\n]%*c", str);

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

break;

commaPos=checkComma(str);

if(commaPos>0)

{

j=0;

for(i=0;i<commaPos;i++)

{

If(str[i]!=' ')

firstWord[j]=str[i];

j++;

}

firstWord[i]='\0';

j=0;

for(i=commaPos+1;i<str[i]!='\0';i++)

if(str[i]!=' ')

{

secondWord[j]=str[i];

j++;

}

secondWord[j]='\0';

printf("First word: %s\n",firstWord);

printf("Second word: %s\n",secondWord);

}

else

{

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

}

}

}

int checkComma(char str[])

{

int i=0;

for(i=0;str[i]!='\0';i++)

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

return i;

return -1;

}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

C++ Only Please 10.15 LAB: Warm up: Contacts You will be building a linked list. Make...
C++ Only Please 10.15 LAB: Warm up: Contacts You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext()...
Please I need all answers about questions in TLC LAB. (Q1) In our lab, what is...
Please I need all answers about questions in TLC LAB. (Q1) In our lab, what is the mobile phase and what is stationary phase? Discuss the polarity of each. (Q2) Do your results suggest that the chemical characteristics of these pigments might differ according to their color? Explain. (Q3) Which of your pigment molecules was the most non-polar? Polar? (Q4) What is the relationship between Rf values and polarity for this experiment? Is this always true? (Q5) Why should you...
In C++ Please comment in all-new lines of code, thank you DO NOT USE ANSWERS THAT...
In C++ Please comment in all-new lines of code, thank you DO NOT USE ANSWERS THAT ALREADY BEEN POSTED, please use code from the assignment Copy-paste will be reported Write a program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120 integers...
This is based on LinkedLists. Avneet Pandey, please do not answer this. Both your previous answers...
This is based on LinkedLists. Avneet Pandey, please do not answer this. Both your previous answers were wrong. Please complete the methods max() and threshold(). I'd greatly appreciate it. There is a type mismatch in my first method and I dont know how to get around it. I've commented on the line with the type mismatch. Please write a correct method in the answer so I can compare it to my wrong method public class Node {    public T...
Please show all work and label everything correctly. 1: Consider the sum of the first n...
Please show all work and label everything correctly. 1: Consider the sum of the first n positive integers that leave a remainder of 4 when divided by 6. FInd and prove a formula for this sum.
Please note that the previous answers were blurred, so can you please make the answers readable....
Please note that the previous answers were blurred, so can you please make the answers readable. Thanks so much, i really appreciate your help. God bless. Question: Elliot Karlin is a 35-year-old bank executive who has just inherited a large sum of money. Having spent several years in the bank's investments department, he's well aware of the concept of duration and decides to apply it to his bond portfolio. In particular, Elliot intends to use $1million of his inheritance to...
PLEASE NOTE: THIS QUESTION HAS BEEN POSTED BEFORE BUT WRONG ANSWERS WERE PROVIDED. PLEASE DO NOT...
PLEASE NOTE: THIS QUESTION HAS BEEN POSTED BEFORE BUT WRONG ANSWERS WERE PROVIDED. PLEASE DO NOT COPY THOSE ANSWERS AND PASTE THEM HERE AGAIN. I WOULD APPRECIATE A FRESH ATTEMPT. HERE IS THE QUESTION: A company has a payment due in three and a half years’ time of $50m. The current market interest rate for this company and this maturity is 2.55%. In order to immunise against this liability, they would like to invest in assets. Two bonds with the...
2. In the previous lab session, you were asked to predict which dibenzoylethylene isomer was the...
2. In the previous lab session, you were asked to predict which dibenzoylethylene isomer was the most polar based on its structure. Was your prediction supported by the observed melting points and TLC Rf values of the two isomers?
11.10 LAB: All permutations of names PLEASE ANSWER IN C++! Write a program that lists all...
11.10 LAB: All permutations of names PLEASE ANSWER IN C++! Write a program that lists all ways people can line up for a photo (all permutations of a list of strings). The program will read a list of one word names (until -1), and use a recursive method to create and output all possible orderings of those names, one ordering per line. When the input is: Julia Lucas Mia -1 then the output is (must match the below ordering): Julia...
****ANSWERS ARE GIVEN BUT MUST SHOW ALL WORK; PLEASE (NO EXCEL OR SPSS)***** ---------------------------------------------------------------------- 1. To...
****ANSWERS ARE GIVEN BUT MUST SHOW ALL WORK; PLEASE (NO EXCEL OR SPSS)***** ---------------------------------------------------------------------- 1. To answer this question, refer to the following hypothetical data collected using replicated measures design. Subject 1 2 3    4 5 6 7 8    9 10 Pre 50 49 37 16 80 42 40 58 31 21 Post 56 50 30 25 90 44 60 71 32 22 A. In a two-tailed test of H 0 using α=0.05, what is p(obtained) for the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT