66.1k views
5 votes
I need help with the following c program:

(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

1 Answer

1 vote

Answer: ↓NEW↓

#include <stdio.h>

#include <string.h>

int main() {

char input[100];

char first[50];

int i, len;

while (1) {

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

fgets(input, 100, stdin);

len = strlen(input);

if (len > 0 && input[len-1] == '\\') {

input[len-1] = '\0';

}

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

break;

}

int found_comma = 0;

for (i = 0; i < len; i++) {

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

found_comma = 1;

break;

}

}

if (!found_comma) {

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

continue;

}

int j = 0;

for (i = 0; i < len; i++) {

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

continue;

}

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

first[j] = '\0';

break;

}

if (j < 50) {

if (input[i] >= 'A' && input[i] <= 'Z') {

first[j] = input[i] - 'A' + 'a';

} else {

first[j] = input[i];

}

j++;

}

}

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

}

return 0;

}

Step-by-step explanation:

↓OLD↓

#include <stdio.h>

#include <string.h>

int main() {

char input[100];

char first[50], second[50];

int i, len;

while (1) {

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

fgets(input, 100, stdin);

len = strlen(input);

if (len > 0 && input[len-1] == '\\') { // remove newline character

input[len-1] = '\0';

}

if (strcmp(input, "q") == 0) { // check if user wants to quit

break;

}

// check if input contains a comma

int found_comma = 0;

for (i = 0; i < len; i++) {

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

found_comma = 1;

break;

}

}

if (!found_comma) { // report error if no comma is found

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

continue;

}

// extract first and second words and remove spaces

int j = 0;

for (i = 0; i < len; i++) {

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

continue;

}

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

first[j] = '\0';

j = 0;

continue;

}

if (j < 50) {

if (input[i] >= 'A' && input[i] <= 'Z') { // convert to lowercase

first[j] = input[i] - 'A' + 'a';

} else {

first[j] = input[i];

}

j++;

}

}

second[j] = '\0';

j = 0;

for (i = 0; i < len; i++) {

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

continue;

}

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

j = 0;

continue;

}

if (j < 50) {

if (input[i] >= 'A' && input[i] <= 'Z') { // convert to lowercase

second[j] = input[i] - 'A' + 'a';

} else {

second[j] = input[i];

}

j++;

}

}

second[j] = '\0';

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

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

}

return 0;

}

This program prompts the user for a string that contains two words separated by a comma, and then extracts and removes any spaces from the two words. It uses a loop to handle multiple lines of input, and exits when the user enters "q". Note that the program converts all uppercase letters to lowercase.

User Guilhebl
by
8.8k points