Thread: Word counting

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    69

    Word counting

    Hello,

    Can you help me why the below code is not printing number of tabs, newlines and spaces.

    Code:
    #include<stdio.h>
    
    int main() {
        int c, nl, nt, ns;
        nl = 0;
        nt = 0;
        ns = 0;
        while ((c = getchar()) != EOF) {
            if (c == '\n')
                ++nl;
            if (c == '\t')
                ++nt;
            if (c == ' ')
                ++ns;
        }
        printf("Blanks: %d\nTabs: %d\nNewlines: %d\n", ns, nt, nl);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Did you redirect input from a file or trigger the end of file condition if entering the input at a command prompt?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    69
    when i run that code, the command prompt screen receiving what i enter but not giving any out put.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It works for me. You probably forgot to trigger the end-of-file condition by say, entering CTRL+D or CTRL+Z on a new line.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    69
    sorry, i don't know what is trigging can show me any example.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    • Run your program.
    • Enter:
      Code:
      hello world
    • Enter CTRL + D

    If the last step does not work, enter CTRL + Z, where CTRL refers to the CTRL key on your board, and the "+" means that you should press both keys simultaneously.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2015
    Posts
    69
    Thank you.

  8. #8
    Registered User
    Join Date
    Apr 2015
    Posts
    69
    yes, i got it by using Borland C, C++ compiler but in Dev-C++ 5.11 it is now working.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think you meant "not working" rather than "now working". How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2015
    Posts
    69
    sorry that is my spelling mistake, it is working fine by using CTRL + Z.

    Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help on counting word.
    By Alexie in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2013, 09:16 AM
  2. Need a fix to word counting
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-25-2011, 06:55 AM
  3. Word counting
    By yuuh in forum C Programming
    Replies: 2
    Last Post: 08-09-2009, 11:47 PM
  4. Word Counting
    By cookie in forum C Programming
    Replies: 18
    Last Post: 06-17-2007, 12:31 PM
  5. Word Counting
    By Achillles in forum C++ Programming
    Replies: 9
    Last Post: 09-11-2002, 02:09 PM