In C, edit the paragraph entered by the user on a single line into a string (cha
ID: 3766694 • Letter: I
Question
In C, edit the paragraph entered by the user on a single line into a string (char paragraph[300]), so that it would be formatted for multiple lines whereby the line length is 40 characters or less as follows: if the last word on the line is going to make the line exceed 40 characters, then that word should start on the next line.
***I am able to enter the paragraph and format it to capitalize the first character of every sentence and to disregard commas as punctuation but I can't figure out how to make the text start a new line after 40(without it cutting off part of a word) nor can I figure out how to check if it is cutting off a word and if it is, pushing that word to start the next line of 40 character text. I would like to be able to do it in a function and then do a function call if possible. Any help would be greatly appreciated! Thank you!
*Code I have so far. (This is just a portion of a larger code but the only part I'm having problems with)
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char paragraph[300];
printf("Please enter a single line paragraph: ");
scanf(" %[^ ]", paragraph);
for(i = 0; i < strlen(paragraph); i++)
{
if(ispunct(paragraph[i]) && paragraph[i] != ',')
{
paragraph[i+2] = toupper(paragraph[i+2]);
}
}
if(islower(paragraph[0]))
{
paragraph[0] = toupper(paragraph[0]);
}
printf("%s ", paragraph);
return 0;
}
Explanation / Answer
At the end check first the space and then the word
if the space is at the end then break the word to the next Line