In: Computer Science
Write a program that echoes the input one word per line. Remove all punctuation and all blank lines.
Code must be written in C. Not c++ or c#.
Input: Use the following Ogden Nash poem to test your program.
The Parsnip
The parsnip, children, I repeat,
Is simply an anemic beet.
Some people call the parsnip edible;
Myself, I find this claim incredible
Note: There are multiple blank lines before and after the title.
There may be multiple blank spaces or tabs between words.
There may be multiple blank lines in the text
Use getchar() and putchar().
You may not use any string handling functions in this program. The only functions you may use are input and output functions, such as getchar and putchar
Output: One word per line left justified. (No punctuation, no blank lines between words)
SOLUTION-
I have solve the problem in C code with comments and screenshot for
easy understanding :)
CODE-
//c code
#include <stdio.h>
#include <conio.h>
int main() {
char story[1024];
int i = 0, j = 0, flag = 0,k,c;
FILE *fptr,*fptr2;
//get input from file
fptr = fopen("sample.txt","r");
while (1)
{
story[i] = fgetc(fptr);
if (story[i++] == EOF)
{
story[i] = '\0';
break;
}
}
//prints in output file
fptr2=fopen("result.txt","w");
for (j = 0; j < i; ++j)
{
if ((story[j] <= 'z' && story[j] >= 'a') || (story[j]
<= 'Z' && story[j] >= 'A') || (story[j] <= '9'
&& story[j] >= '0'))
{
fputc(story[j],fptr2);
flag = 1;
}
else
{
if (flag==1)
{
fputc('\n',fptr2);
flag = 0;
}
}
}
fclose(fptr);
fclose(fptr2);
return 0;
}
--------------------------------------------------------------------------
Input file=sample.txt
The Parsnip
The parsnip, children, I repeat,
Is simply an anemic beet.
Some people call the parsnip edible;
Myself, I find this claim incredible.
---------------------------------------------------------------------------
Output file=result.txt
The
Parsnip
The
parsnip
children
I
repeat
Is
simply
an
anemic
beet
Some
people
call
the
parsnip
edible
Myself
I
find
this
claim
incredible
SCREENSHOT-
Input file=sample.txt
Output file=result.txt
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------