Thread: getchar putchar

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    Thumbs up getchar putchar

    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."





    Code:
    #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

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You could just keep track of the last character you printed. If it was a newline then don't print another one:
    Code:
    if(c!=' ' && c!='\t' && c!='\n' && c!='.' && c!=',' && c!=';' )
      putchar((lastchar = c));
    else if(lastchar != '\n')
      putchar((lastchar = '\n'));
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    Thumbs up getchar putchar

    Thanks.
    But what if I encounter white space, tab , semicolon or.. then it just goes to the next line. how to address this problem?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Extend the idea that itsme86 gave you. If the last character is a newline or...

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    11
    The
    Parsnip



    The
    parnip

    children


    I
    repeat

    Is
    simply
    an
    anemic
    beet

    Some
    people
    call
    the
    parsnp
    edible

    Myself



    I
    find
    this
    claim
    incredible[/QUOTE]

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    11
    I would like to close ths topic how can I do that ?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop replying to it.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Unreformed cross-poster
    getchar putchar - C

    Previous sins
    check characterin 2D array
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar putchar: a word on each line
    By krazymanrebirth in forum C Programming
    Replies: 27
    Last Post: 09-29-2009, 02:57 PM
  2. Calculator using getchar, putchar
    By arlen20002000 in forum C Programming
    Replies: 5
    Last Post: 03-26-2008, 10:37 AM
  3. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  4. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM
  5. Replies: 2
    Last Post: 01-10-2002, 07:42 PM