Please show as many comments as you can that describe what is going on. No matte
ID: 3632657 • Letter: P
Question
Please show as many comments as you can that describe what is going on. No matter how basic. I'm new to programming and struggling with these programs. Also, this program has to be written in C code not C++. Here is the question and some trial runs showing what the program must input and output exactly. Thank you all for your help.Write a program sentence.c that prompts the user for a sentence (ending in a period) and prints it, possibly in multiple lines, so that:
-each line has at most 15 characters,
-lines can be broken only at a space, and this space does not get printed.
You can assume that all entered words are less than 15 characters long, and that the length of the entered sentence is at most 500 characters.
Sample runs:
(~)$ a.out
Sentence: C programming is very cool.
C programming
is very cool.
(~)$ a.out
Sentence: Thanksgiving is always on a Thursday.
Thanksgiving is
always on a
Thursday.
(~)$ a.out
Sentence: The rain in Spain stays mainly in the plain.
The rain in
Spain stays
mainly in the
plain.
(~)$
Hint: One way to implement this program is to read each word using "%s" into a character array of length 15, check the word's length and whether it ends with a ".", and then print it on the same or a new line depending on how many characters have already been printed on that line.
Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{char c[16];
int p=0;
size_t len;
printf("Sentence: ");
do
{
scanf("%s",&c);
len=strlen(c);
if(p==0)
{printf("%s",c);
p+=(len+1);
}
else if(p+len+1<15)
{printf(" %s",c);
p+=len;
}
else
{printf(" %s",c);
p=len;
}
}while(c[len-1]!='.');
getch();
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.