I would like to write a program with getchar and putchar that read my input and prints one word per line and ignores all semicolon, comma , dot , newline, tab and space.
Here is my input:

"The Parsnip

The parnip, children, I repeat,
Is simply an anemic beet.
Some people call the parsnp edible;
Myself, I find this claim incredible."

#include<stdio.h>
int main()
{

 int c;

 while ((c = getchar()) != EOF) 
   {
	  
		  if(c!=' ' && c!='\t' && c!='\n' && c!='.' && c!=',' && c!=';' )
	       putchar(c);
	   else
		   putchar('\n');

  
   }

I would like to do something that my program prints world after each other in the newline, one word per each line without any space between words.
here is my output:I would like to remove space between words(empty lines).
Any suggestion or help s highly appreciated.


The
Parsnip

The
parnip

children


I
repeat

Is
simply
an
anemic
beet

Some
people
call
the
parsnp
edible

Myself

I
find
this
claim
incredible

Recommended Answers

All 10 Replies

iniatialise flag=0
instead of

else
     putchar('\n');flag=0;

use

else if(c!=' ' && c!='\t' && c!='\n' )
     if(flag==0)
     {
           putchar('\n');
           flag=1;
      }

Could you please write the full code because I culd not understand whay you mean?
Thanks.

Why not you try getch(),
getchar() always wait for enter key to be wait

int c;
while ((c=getch())!=0)
   {
	  if(c!=32 && c!=9 && c!=13 && c!=46 && c!=44 && c!=59 )
	       putchar(c);
	   else
              putchar('\n');
   }

Hope it will be work for you other wise feel free to ask your problem

commented: Becuse getch() is NOT standard C. -3

It still prints the empty lines between my codes , I would like to get rid of those lines.just one word per line. For example remove empty lines between Parsnip and The.
I have to use getchar and putchar.
The
Parsnip

The
parnip

children


I
repeat

Is
simply
an
anemic
beet

Some
people
call
the
parsnp
edible

Myself

I
find
this
claim
incredible

You need to use a flag to keep track of whether or not a \n has already been output.

Start with the flag set to TRUE.
When you output a character, set the flag to FALSE.
When you want to output \n, test the flag.
If the flag is FALSE, output \n and set it to TRUE; if TRUE do nothing.

Hello chess 2009,
Empty lines are displayed if there are multiple spaces between the two words.
Try this logic in the if else block.
If there are one or more spaces between the set of characters,the cursor should go to next line.Try this...

if(c!=' ' && c!='\t' && c!='\n' && c!='.' && c!=',' && c!=';' )
   { putchar(c);
     b=0;    //counter variable
   }
 else
 {if(b==0)       // if b==0 the cursor goes to next line.If multiple spaces are                  
  {putchar('\n');//there b becomes grater than zero.Ultimately avoiding empty spaces
  }
  b=b+1;
}

Thanks to all of you here is the code that I wrote , it is better than before but it does not prins some characters.

#include<stdio.h>
int main()
{
  int c,flag=0;
  while((c=getchar())!=EOF)
  {
	  if(flag==1)
	  {
		  if(c=='\n' || c=='\t' || c==' ' || c==';'  || c==',' || c=='.')
		  {
			  putchar('\n');
		      flag=0;
			  
		  }
	      
		  else
		  {
			  putchar(c);
		      flag=1;
		  }
	  }
	  else
	  {
		  if(c=='\n' || c=='\t' || c==' ' || c==';'  || c==',' || c=='.')
		  {
			  getchar();
		  }
		  else 
		  {
			  flag=1;
			  putchar(c);
		
			
		  }
	  }
  }
  return 0;
}

unfortunately my output is
he
Parsnip
he
parnip
hildren
I
repeat
s
simply
an
anemic
beet
ome
people
call
the
parsnp
edible
yself
ind
this
claim
incredible


As you can see it for example it did not print the capital T for the.
Any suggestion?

Probably because you ELSE clause does a getchar() and throws away whatever it read. I doubt you need that particular ELSE.

And your IF structure is inside out. You should be checking your characters first. Then check your flag only when you need to.

How can I erase my code ? Because I really need to do that .
Thank you.

In Explorer:
Right click on the file containing the code
Choose DELETE.

commented: I tried that with My Computer/C: drive and now my computer doesn't work ;) +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.