C program: 12.21 Ch 9 Warm up: Parsing strings (C) (1) Prompt the user for a str
ID: 3920711 • Letter: C
Question
C program:
12.21 Ch 9 Warm up: Parsing strings (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:
Code to edit:
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argv, char** args)
{
// Declare the required variables.
char *s1;
char s2[50], s3[50];
size_t size = 50;
int flag = 0;
// Allocate the memory.
s1 = (char *)malloc(size * sizeof(char));
// Prompt the user to enter the input.
printf("Enter input string: ");
getline(&s1, &size, stdin);
// Start the loop.
while (1)
{
// Check if the user wants to quit.
if (strlen(s1) == 2)
{
if (s1[0] == 'q')
{
// Exit the program.
exit(0);
}
}
int i, j = 0;
// Start the loop till end of the string.
for (i = 0; s1[i] != ''; i++)
{
// Break the loop if comma is found.
if (s1[i] == ',')
{
// Set the flag and break the loop.
flag = 1;
break;
}
// Otherwise, enter the character in the
// new string.
if (s1[i] != ' ' && s1[i] != ',')
{
s2[j++] = s1[i];
}
}
// Insert the null character.
s2[j] = '';
// Update the values.
i++;
j = 0;
// Check the value of the flag.
if (flag == 1)
{
// Start the loop till end of the string.
while (s1[i] != '')
{
// Enter the characeter in the new string.
if (s1[i] != ' ')
{
s3[j++] = s1[i];
}
i++;
}
// Insert the null character.
s3[j] = '';
// Display the strings.
printf("First word: %s", s2);
printf(" Second word: %s", s3);
}
// Otherwise, display the error message.
else
{
printf("Error: No comma in string.");
}
// Prompt the user to enter another string.
printf(" Enter input string: ");
getline(&s1, &size, stdin);
}
}
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFFER_SIZE 1000
void trim(char *str) {
int i, begin = 0;
int end = strlen(str) - 1;
while (isspace((unsigned char) str[begin])) {
begin++;
}
while ((end >= begin) && isspace((unsigned char) str[end])) {
end--;
}
for (i = begin; i <= end; i++) {
str[i - begin] = str[i];
}
str[i - begin] = '';
}
int main() {
char input[BUFFER_SIZE];
int i =0;
while (1) {
printf("Enter input string : ");
fgets(input, BUFSIZ, stdin);
if (strcmp(input, "q ") == 0) {
break;
}
for (i=0; i<strlen(input); i++) {
if (input[i] == ',') {
char first_word, second_word;
char *search = ",";
first_word = strtok(input,search);
second_word = strtok(NULL,search);
trim(first_word);
trim(second_word);
printf("First Word : %s ", first_word);
printf("Second Word : %s ", second_word);
exit(0);
}
}
printf("Error: No comma in string. ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.