## IN C LANGUAGE ## Create a program that accepts and formats a paragraph from t
ID: 3823718 • Letter: #
Question
## IN C LANGUAGE ##
Create a program that accepts and formats a paragraph from the user as follows:
Ask the user for the length of the paragraph in characters. Repeatedly ask the user for the number of characters and only except a number more than 80 and less than 500.
Create three character arrays:
Create an array named “temp” of a size that fits the paragraph.
Create an array “name” with length 50.
Create an array “pa” with length 600.
Create a function called menu with the following options:
C to clear all character arrays by calling a function named “wipe” on each of the three arrays.
N to enter the name of the author, first and last names separated by a space, and store it in “name”.
F to call function “heading”. this function will change the first letter in each, first and last name, to upper case and create the following string in “pa” if user input first name is sam and last name is russel:
Paragraph by Einstein.
P to enter a new paragraph into the “temp” array. The paragraph should be entered in one scanf statement using “%[^ ]” instead of “%s” as a format string or a gets statement.
R to format the string in “temp” so that the first letter of the paragraph and each sentence is upper case, and each sentence is on one line.
V to combine the contents of name and temp in pa in order to produce the desired output(see example output below).
S to display the contents of “pa” by calling the following function on each of the two arrays.
void paragraph( char *cat)
{
printf("%s ", cat);
}
X to exit the program.
Note 1: All menu options should work for upper and lower case input.
Explanation / Answer
PROGRAM CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void wipe(char temp[], char name[], char pa[])
{
temp = "";
name = "";
pa = "";
}
void paragraph( char *cat)
{
printf("%s ", cat);
}
void heading(char name[], char pa[])
{
if(strcmp(name, "sam russel")==0)
pa = "Paragraph by Einstein.";
name[0] = toupper(name[0]);
int index = 0;
while(name[index] != ' ')
index++;
name[++index] = toupper(name[index]);
}
char* format(char temp[])
{
int index = 0;
int newIndex = 0;
char newTemp[600];
int length = strlen(temp);
while(index<length)
{
if(temp[index] == '.')
{
newTemp[newIndex] = ' ';
while(temp[index] == ' ' && index<length)
index++;
newTemp[++newIndex] = toupper(temp[index]);
}else
newTemp[newIndex] = temp[index];
newIndex++;
index++;
}
return &newTemp;
}
void menu(char temp[], char name[], char pa[])
{
char menuChoice;
while(1)
{
char *firstName, *lastName;
printf("C - Clear all arrays N - Enter name of the author F - Update name with uppercase");
printf(" P - Enter new paragraph R - Format the paragraph V - combine all the contents S - Display the contents X - Exit");
printf("Enter your choice: ");
scanf("%c", &menuChoice);
switch(menuChoice)
{
case 'c':
case 'C': wipe(temp, name, pa); break;
case 'n':
case 'N': printf("Enter first name of the author: ");
scanf("%s", firstName);
printf("Enter last name of the author: ");
scanf("%s", lastName);
strcpy(name, firstName);
strcat(name, " ");
strcat(name, lastName);
break;
case 'f':
case 'F':heading(name, pa); break;
case 'p':
case 'P': printf("Enteer new paragraph: ");
scanf("%[^ ]", temp);
break;
case 'r':
case 'R': strcpy(temp , format(temp)); break;
case 'v':
case 'V': strcat(pa, " ");
strcat(pa, name);
strcat(pa, " ");
strcat(pa, temp); break;
case 's':
case 'S': paragraph(name);
paragraph(temp);
break;
case 'x':
case 'X': exit(0);
}
}
}
int main(void) {
int len = 0;
while(len<80 || len>500)
{
printf("Enter the length of the paragraph (80 to 500): ");
scanf("%d", &len);
}
char temp[len], name[50], pa[600];
menu(temp, name, pa);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.