Thread: Why my 'count the characters' program doesn't work?

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

    Why my 'count the characters' program doesn't work?

    I was trying to make a c program to count the number of characters entered, and here is the code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    
    
    {
    int i=0;
    while(getchar()!= EOF)
        ++i;
    printf("%d\n", i);
    }


    Well this doesn't work. After entering the string there is no value printed. It also doesn't work if I declare char c and use scanf.

    But the following code works :
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int main()
    {
    int cl=0,i;
    char str[20];
    gets(str);
    for(i=0; str[i]!= NULL ; i++)
    {
        cl = cl +1;
    }
    printf("%d", cl);
    
    }
    Why I don't know. Btw I am using codeblocks IDE with gcc compiler.


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well to signal an EOF condition in your terminal, you should press ctrl-z (if you're on windows), or ctrl-d (Linux or other BSD/Unix like operating systems).


    Also, never use gets()
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    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.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You're asking it to count characters until EOF, which is not end-of-line.
    So you need to signal the "end-of-file" from the keyboard, which is ctrl-d on *nix, ctrl-z on Windows. It may need to be entered at the beginning of a line (or perhaps entered twice).

    To count to the end of a line, compare to '\n' instead.
    In the following, signal eof to quit.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        int i = 0, c = 0;
        while (c != EOF) {
            i = 0;
            while ((c = getchar()) != EOF && c != '\n')
                ++i;
            if (c != EOF)
                printf("%d\n", i);
        }
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Feb 2017
    Posts
    11
    Well I tried signalling eof by pressing ctrl-z but it just prints ^z on the console. :\

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Noobpati View Post
    Well I tried signalling eof by pressing ctrl-z but it just prints ^z on the console. :\
    So I assume you're on Windows. Did you press ctrl-z at the beginning of a line as I said?

  6. #6
    Registered User
    Join Date
    Feb 2017
    Posts
    11
    To count to the end of a line, compare to '\n' instead.
    I see, but if I use '\n' to signal exit how can I write a program to count the number of lines.
    Also it tried entering ctrlz at the start and at the end of line and it prints zero in that case but if I write ctrlz after pressing enter that is , on the new-line then it prints value greater than the number of characters given. I guess it also counts ^z.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Noobpati View Post
    I see, but if I use '\n' to signal exit how can I write a program to count the number of lines.
    I didn't say to use '\n' to signal exit. Try the program that I posted. For me (on linux) it prints the count of the number of characters in the line just entered, and when I press ctrl-d on a new line it simply exits.

    Also it tried entering ctrlz at the start and at the end of line and it prints zero in that case but if I write ctrlz after pressing enter that is , on the new-line then it prints value greater than the number of characters given. I guess it also counts ^z.
    Well I don't use windows, so I can't help you there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Characters counter doesn't work
    By an96 in forum C Programming
    Replies: 6
    Last Post: 06-19-2015, 11:07 AM
  2. Replies: 13
    Last Post: 06-07-2013, 05:45 PM
  3. Replies: 56
    Last Post: 04-26-2013, 03:13 PM
  4. Replies: 7
    Last Post: 02-20-2013, 12:28 AM
  5. Replies: 1
    Last Post: 12-07-2010, 06:53 AM

Tags for this Thread