Thread: Word counting

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Unhappy Word counting


    I'm really new at programming but i want to learn and study out the best I can. Maybe I won't be able to do the basics of programming during this year but i would like to.

    My code shoul count words according to parameters I choose. So first when I count words I get always a less words as I have. But I don't know how to do it correctly

    Code:
     int wordcounting(void){
    
         char c;
         int s;
            s=0;
         while ((c = getchar ()) != EOF) {
            if (c == ' ' || c == '\n' || c == '\t')
                s++;
            }
            printf("Number of words%d",s);
    
    
         }
    Then this segment of program shoul print out every word on a different line and plus if the word has more than 20 caracters then it should write out only the first 20.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int c,caracters;
        caracters = 0;
        while ((c = getchar()) != EOF)
            ++caracters;
           while (caracters <= 20)
        putchar (c);
    
    }
    The third part is printing out words which contain the character what you put into a parameter

    So It will look like this?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int c;
        int d;
        scanf("%d",&d);
        while ((c = getchar()) != EOF)
        if (c = d)
            putchar (c);
    }
    I should work with parameter comparing them but I don't know how. Mainly if there are more than 2 parameters for countig words and for printing out words which have a the caracter I have chosen.

    I appreciate any help. I am desperate and really stressed out :brrr:
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    There are a couple of issues I see with the syntax.

    Note that = is the assignment operator which simply assigns into the left-hand side. The value of the assignment operation is the value that is being assigned. Remember that C treats non-zero numbers as true and zero as false.

    Also make sure that you surround the body of your while loop with { and }. Otherwise only the first line after the while loop will be the body.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0
    Hello guys. I'm still working on my code. This should write out each word on a new line(words are numbers as well + few characters) ant it should write out just the first 40 characters of each word. But this code writes me out only the first 40 characters and then nothing.

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    
    int c,i;
    i = 0;
    while ((c = getchar()) != EOF) {
            ++i;
            if ( (c <= 44) || ((c >= 58) && (c <= 64)) || ((c >= 91) && (c <= 94)) )
                    printf("\n");
    
            else if(i <= 40)
                    putchar(c);}
    
    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    25
    Rep Power
    0
    Hi kristian1112,

    If I may ask what do you want to achieve? And are all these codes a single file or what. Am asking for clarity sake though.

    If you intended to count words and character. You can achieve that like so:
    Code:
    #include<stdio.h>
    
    int main(void){
    
       int c, counter = 0, count_character = 0;
    
        while((c = getchar()) != EOF) 
           if( c == '\n' || c == ' ' || c == '\t')
              ++counter;  
           else
    	  ++count_character;
        
        printf("Number of words: %d\nNumber of Character(s): %d\n", counter, count_character);
        
     return 0;
    }
    Those are the ones, I could make out what you intend to achieve. Am sure if you are more explicit on what you want on others maybe advice could be given.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0
    I have a complex program for counting words. And these are some functions from my program but it doesn't work much and should finish it until tomorrow or until the next weekend. I have a problem how to reset the counter when I want to print out only first 40 characters of a word. It's that segment:


    Code:
    #include <stdio.h>
    
    int main()
    
    {
    
    int c,i;
    i = 0;
    while ((c = getchar()) != EOF) {
            ++i;
            if ( (c <= 44) || ((c >= 58) && (c <= 64)) || ((c >= 91) && (c <= 94)) )
                    printf("\n");
    
            else if(i <= 40)
                    putchar(c);}
    
    }
    It should reset the counter after it finds a blank character as ' ' or \n and so on. I tried to put it into my code but i got an infinite loop..
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Code:
    if ( (c <= 44) || ((c >= 58) && (c <= 64)) || ((c >= 91) && (c <= 94)) )
    While that code can work, anyone trying to read that, including yourself, would have difficulty understanding what it's doing. They'd be required to hunt down an ASCII table to see what characters those are supposed to be. And if you had made a mistake in looking up those ASCII codes when you wrote the code, you and everybody else would be much less likely to spot the bug. It would be much better, more readable, less error-prone, and easier to debug if you just used the characters; eg:
    Code:
    if ( (c <= '.') || ((c >= ':') && (c <= '@')) || ((c >= '[') && (c <= '^')) )
    Originally Posted by kristian1112
    It should reset the counter after it finds a blank character as ' ' or \n and so on.
    I do not see where you are resetting any counter.

    Originally Posted by kristian1112
    I tried to put it into my code but i got an infinite loop..
    Code:
    while ((c = getchar()) != EOF) {
    How exactly are you entering EOF from the keyboard?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0
    Thank you I'll change the ASCII codes.

    I tried in this way reset the counter after the program printed out the output,but it won't work. This way it prints every character not just the first 40

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    
    int c,i;
    i = 0;
    while ((c = getchar()) != EOF) {
            ++i;
            if ( (c <= 44) || ((c >= 58) && (c <= 64)) || ((c >= 91) && (c <= 94)) )
                    printf("\n");
    
            else if(i <= 40)
                    putchar(c);
                    i = 0;
                    }
    
    }
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    You are misleading yourself with your own terrible indentation style. Here is what your code looks like when properly indented:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int c,i;
        i = 0;
        while ((c = getchar()) != EOF) 
        {
            ++i;
            if ( (c <= 44) || ((c >= 58) && (c <= 64)) || ((c >= 91) && (c <= 94)) )
                printf("\n");
            else if(i <= 40)
                putchar(c);
    
            i = 0;
        }
    
    }
    That K&R style (which I dislike very much) hides the open brace at the ends and in the middle of lines, so it depends absolutely on the programmer being absolutely consistent in his indentation, including his paying total attention to indentation levels. You are not doing that, which makes your code confusing and misleading. You are hiding the structure of your program. And you have fallen into the trap that you had set yourself.

    I just used the Allman style (please follow that link). It brings the open brace out at the same level of indentation as its matching close brace, so it can remain somewhat readable even when you (personally) mess up the indentation, whereas K&R becomes totally unreadable.

    Now that you can finally see what your own code looks like, you can clearly see that at the end of every single loop you reset that counter. So of course the counter can never exceed 40; you won't even let it exceed 1.

    You need to indent your code properly. You need to do that for yourself! If you cannot read your own code, then you are sabotaging yourself.

IMN logo majestic logo threadwatch logo seochat tools logo