word count troubles

This is a discussion on word count troubles within the C Programming forums, part of the General Programming Boards category; Hey, I was just wondering if you guys could help me better understand this concept: given: Code: int bubble(int a[], ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67

    word count troubles

    Hey, I was just wondering if you guys could help me better understand this concept:

    given:

    Code:
    int bubble(int a[], int n)
    and:

    Code:
    int a[8] = {1,2,3,4,5,6,7,8}
    we can invoke the bubble funtion by using:

    Code:
    bubble(a,8)
    Correct?


    Now, if I have the user input a sting into a program such as:

    Code:
    #define  MAXSTRING   100
    
    
    void word_cnt (char a[], int n)  // not sure if that is correct//
    
    int main(void)
    {
          char     c, a[MAXSTRING];
          int        i;
    
         printf("\nPlease enter your favorite string. ");
         for(i = 0,(c = getchar()) != '\n'; ++i){
              a[i] = c
             }
              a[i] = '\0';  
             word_cnt(a,MAXSTRING)
    }
    Is this a correct way of calling a function and passing the string to it?

    Is it silly to use a function and not just count the words in main(void)?

    I'm also not sure how to count through the characters in a string in another function.

  2. #2
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    You do not actually pass an array to function - rather you pass the address of the first element. The array name is a pointer set to that address.

    In a function header, the char a[] is equivalent to char* a, which is a pointer that holds the address of the array that is passed to it.

    In the function you can use the same method of accessing an array as you would had it not been in a function because this (accessing an element of the array)
    Code:
    a[n]
    is the same as
    Code:
    *(a+n)
    You can use either, even if it is main().

  3. #3
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Yes it's correct but the lack of semicolons and that method you've got for getting a string isn't. Use fgets(a, MAXSTRING, stdin) instead.
    And by counting characters, do you mean something like strlen()?

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    What I'm suppose to do it count the words in a string input by a user in a fuction word_cnt(). I'm suppose to use array notation with subscripts to do this, instead of using pointers to process the string. I just wasn't sure how to use a function if I couldn't use pointers. It's a little confusing to me.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    This is what I came up with for my word_cnt function. I know the for statment isn't the best way, but that is what i'm suppose to use so bear with me:


    Code:
    void word_cnt (char a[], int n)
    {
        int i, cnt = 0;
        
        for( i = 0; i != '\0'; ++i){
             while(isspace (a[i]))          //skip white spaces
             ;                                       
             if (a[i] != '\0'){                   //finds a word
               ++cnt;
             while (!isspace(a[i]) && a[i] != '\0') //skips word when found
             ;
    }
    }
             printf("The number of words in your string is:%d", cnt");
    }
    I'm a newbie to C programming, so if this has terrible style or is rediculously wrong, go easy on me

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    So, have you tried it? Think about your loop condition(s). In your while loops: what might change true to false? You can do this without nested loops if you have a variable to keep track of whether or not you are currently in a word or whitespace.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    No, I'm sorry. I haven't been able to try it yet. I will be able to this evening. I just wanted to make sure that I was on the right track with what I was coming up with. I'm not really very good at this stuff yet, but it's coming along.

    So do you think I have to much stuff in my word_cnt function?

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    It is more complicated then it needs to be, and doesn't work.
    I'm sure you'd notice these errors when you actually try to compile and run
    Code:
    for( i = 0; [a]i != '\0'; ++i){
    the first time you reach a space you'll have an infinte loop
    Code:
    while(isspace (a[i]))          //skip white spaces
             ;

    Something like this should work
    Code:
    int word_cnt(char[a])
    {
      int cnt = 0
      char previous = ' '
    
      for each position in the string{
        if current position is not a space and previous position is a space
          cnt++
        previous = current
      }
    
      return cnt
    }

  9. #9
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    is char [a] correct?

    or should it be char a[] ?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    char a[]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    printf("The number of words in your string is:%d", cnt");
    You have an extra double quote there.
    Code:
    while(isspace (a[i]))          //skip white spaces
             ;
    To get this to work you need to increment i in the while loop.

    BTW, you can use sizeof to find the size of an array:
    Code:
    int array[10] = {1, 2, 3};
    word_cnt(array, sizeof(array)/sizeof(array[0]));
    Should you be passing an int array to a word counting function?

    isspace() and family are in <ctype.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    oh, for my word count function, I will be counting the words input by the user in a string, so I was wondering if I passed the address correctly as:

    Code:
    word_cnt(char a[])
    or should it be

    Code:
    word_cnt(char a[], int n)
    where n is the size, but i'm confused here because i don't know the size of the string yet. Do I need to find the size of the string before I start counting words?

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Well, look at how much you are using n in your code. Since the function takes a string and you find the end via the null termination, passing a size parameter is not necessary.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    you're right. when looking at my function, i don't even use n. silly question on my part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. word count help?
    By n3cr0_l0rd in forum C Programming
    Replies: 13
    Last Post: 02-07-2009, 08:29 AM
  2. word count
    By unity_1985 in forum C Programming
    Replies: 3
    Last Post: 07-29-2007, 11:34 AM
  3. Replies: 7
    Last Post: 06-16-2006, 10:23 PM
  4. Replies: 5
    Last Post: 09-28-2004, 01:38 PM
  5. Word Count PlS HELP
    By Achillles in forum C++ Programming
    Replies: 12
    Last Post: 09-11-2002, 01:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21