Thread: How c=getchar() works?

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    13

    How c=getchar() works?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(){
        int c,lastc,gap;
    
        lastc='a';
        gap=0;
    
        while((c=getchar())!=EOF){
            if((c==' ' || c=='\t' || c=='\n') && (lastc!=' ' || lastc!='\t' || lastc!='\n')){
                gap++;}
    
            lastc=c;
    
    
        }
    
        printf("%d",gap);
    
    
    
    return 0;
    }
    What i understand from this code is it should count gaps in between words but when you execute, it gives how many times you entered blank, tab and endline.. Anybody can help me? I am stuck.
    thanks.
    Last edited by sleftar; 08-29-2015 at 11:19 AM.

  2. #2
    Registered User
    Join Date
    Jan 2014
    Posts
    13
    OK i figured it out...

    Code:
    (lastc!=' ' || lastc!='\t' || lastc!='\n')
    this part should be like

    Code:
    (lastc!=' ' && lastc!='\t' && lastc!='\n')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() help
    By Sylar Persie in forum C Programming
    Replies: 5
    Last Post: 07-08-2013, 03:11 AM
  2. getchar()
    By bbray in forum C Programming
    Replies: 4
    Last Post: 09-08-2011, 05:13 AM
  3. getchar() from c to c++ ?
    By neoragexxx in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2006, 12:13 PM
  4. getchar() and '\b'
    By snfiddy in forum C Programming
    Replies: 2
    Last Post: 11-30-2005, 05:52 PM
  5. need help with getchar
    By sdogg45 in forum C Programming
    Replies: 17
    Last Post: 02-26-2004, 05:39 PM