Thread: Trying to write a program that counts: blank spaces, numbers and letter from a line

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    Trying to write a program that counts: blank spaces, numbers and letter from a line

    I'm trying to write a program that counts the blank spaces, letters and numbers in a user-input line.

    For example:
    A fox has 4 legs

    Total blank spaces: 4
    Total numbers: 1
    Total Letters: 11

    The book I'm reading has the following C code:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    void main(int argc, char **argv)
    {
        int c;
        int ws = 0;
        int dig = 0;
        int al = 0;
        while ((c = getchar()) !=EOF)
        {
            if (isdigit(c)) dig++;
            else if (isalpha(c)) al++;
            else if (isspace(c)) ws++;
        }
        printf("Blank Spaces: %d\n", ws);
        printf("Digits: %d\n", dig);
        printf("Alphabitical Chars: %d\n", al);
    
        }
    Now whenever I build and run, the program allows me to type anything infinitely.
    The problem is how do I stop from typing and make the program count the numbers, blank spaces and chars?.
    Whenever I close it I get: Process terminated with status -1073741510

    PS) The book is really, really crap and does not give further explanations, just the question and the answer, but I have no other choice to read other books.
    I will be giving exams in 15 days it has around 20 questions in C programming language (total questions: 450) and I don't have the funkiest idea/clue of C.
    To be honest, I do not have enough time to learn C programming language.
    I'm really sorry for making topics for each program I'm trying to make.
    The book is bad-written, it has lots of mistakes in the code
    Last edited by MariosX; 09-02-2011 at 04:25 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MariosX View Post
    how do I stop from typing and make the program count the numbers, blank spaces and chars?.
    By entering EOF of course.


    Quzah.
    Last edited by quzah; 09-02-2011 at 04:26 PM. Reason: fixed link
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Quote Originally Posted by quzah View Post
    By entering EOF of course.


    Quzah.
    Please can you be so kind and make the corrections in the code?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Please can you be so kind and read the (fixed) link I provided?


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

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MariosX View Post
    Please can you be so kind and make the corrections in the code?
    I take it you've read this forum's Homework Policy

    Yes we will give you pointers tips and such.
    Yes we will help you sort out problems in you code.

    No we will not fix your code for you...

    Follow the link Quzah gave you, and if you still have problems, ask explicit questions and we'll see what we can do.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Please can you be so kind and read the (fixed) link I provided?
    Quzah.
    Or this one... End-of-file - Wikipedia, the free encyclopedia

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    A ok, the code was ok then but I still got "Process terminated with status 22" changed 3rd line to
    int main(int argc, char **argv)
    and added a "return 0"

    All I had to do was to press Ctrl + Z in DOS.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MariosX View Post
    A ok, the code was ok then but I still got "Process terminated with status 22" changed 3rd line to
    int main(int argc, char **argv)
    and added a "return 0"

    All I had to do was to press Ctrl + Z in DOS.
    Did you really mean to say DOS (as opposed to "a DOS Shell") ?

    My gosh, nobody has used MS_DOS in almost 20 years!


    Also since you are using neither argc nor argv in your program the correct form of main is:
    Code:
    int main (void)
      {
    
          // your code here
    
         return 0; 
    }

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    Did you really mean to say DOS (as opposed to "a DOS Shell") ?

    My gosh, nobody has used that in almost 20 years!
    I mean I run the programs in command prompt in Windows 7.
    My book is really old..... or the author was a grandpa I do not know.
    I'm learning stuff about Windows 3.1, Windows 95, MS-DOS, Unix, Windows NT 4.0..... sometimes they make me want to puke.
    For example: One question asks: What are the most common used internet connection speeds? The answer is, of course: PSTN 56 kbps, ISDN 64kbps. No Dual ISDN, aDSL, vDSL, Cable, T1 etc.
    Well sorry to anybody, I hope no offense was taken, thanks for directions I did not read the rules.
    But now I know how to ask properly.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MariosX View Post
    I mean I run the programs in command prompt in Windows 7.
    Ahh... OK. These days we call it a command shell... FWIW.

    The reason I asked is that we've had quite a number of people (dominantly from India) come here over the past while who are working with 20 year old compilers on 25 year old systems... and don't seem to actually know how time warped they are... Seems some schools haven't kept up...

    If you're looking for a more up to date C tutorial or text book, there are quite a few good recommendations in the sticky message at the top of this forum. (Newer posts, newer information, for the most part). I got started with "Teach yourself C in 21 days"... not everyone's favorite, but it worked ok for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-27-2010, 12:42 PM
  2. Check for blank spaces in a string
    By BENCHMARKMAN in forum C Programming
    Replies: 19
    Last Post: 03-12-2008, 06:14 PM
  3. Replies: 4
    Last Post: 12-01-2007, 04:10 PM
  4. Replies: 3
    Last Post: 04-06-2005, 11:04 AM
  5. removing blank spaces
    By Bash in forum C Programming
    Replies: 1
    Last Post: 04-10-2002, 09:37 AM