Thread: Iterating through an array

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116

    Iterating through an array

    I'm working on another program from my book and I've come across a little snag.
    The goal of the program is to take in a sentence given by the user and count the number of words. (actually, it takes in the number of spaces between words up to the null character)
    The problem I'm having is I'm not quite sure how to iterate through the array to count the characters themselves.
    This is what Ive completed so far.
    While you guys are looking at it, please feel free to critique the code. Lemme know what i could change to make it better.
    Thanks guys.

    Code:
    //program to count words in a user supplied string
    #include <stdio.h>
    
    char line [100];
    char sentence = 0;
    char wordcount(char setence); 
    int numberOfWords = 0;
    
    int main()
    {
    	printf("Enter a line and I will count the number of words: ");
    	fgets(line, sizeof(line), stdin);
    	printf("There are %d words in the sentance.", numberOfWords);	
    }
    
    //function: wordcount
    //this function takes in the sentance variable, counts to each space character defined as ' ' and prints out the number of times 
    //it saw the ' ' (space) character.
    char wordcount(char line)
    {
    	for(line <= sizeof(line), line == "", ++numberOfWords)
    		{
    			return numberOfWords;
    		}
    }

  2. #2
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    Code:
    //program to count words in a user supplied string
    #include <stdio.h>
    #include <string.h>
    
    char line [100];
    char sentence = 0;
    int wordcount(char *line); 
    int numberOfWords;
    
    int main()
    {
    	printf("Enter a line and I will count the number of words: ");
    	fgets(line, sizeof(line), stdin);
    	printf("There are &#37;d words in the sentance.", wordcount(line));
    
                    return 0;	
    }
    
    //function: wordcount
    //this function takes in the sentance variable, counts to each space character defined as ' ' and prints out the number of times 
    //it saw the ' ' (space) character.
    int wordcount(char *line)
    {
    	for(numberOfWords = 0; strtok(line, " "); ++numberOfWords);
    
                    return numberOfWords;
    }
    note that there are numerous corrections, you obviously need to get yourself a good book and do some more reading. I haven't tested the above code but it should work to do what you want it to.
    Last edited by Bleech; 11-10-2007 at 01:32 AM.

  3. #3
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    Quote Originally Posted by MikeyIckey View Post
    I'm working on another program from my book and I've come across a little snag.
    if this code is from a book.. you better change your book and start afresh
    Quote Originally Posted by MikeyIckey View Post
    The goal of the program is to take in a sentence given by the user and count the number of words. (actually, it takes in the number of spaces between words up to the null character)
    The problem I'm having is I'm not quite sure how to iterate through the array to count the characters themselves.
    to iterate a char array you will use a loop..
    if you know how it works.. then the condition for that loop should be to check that the char stored in the array is '\0' before it again enters into the loop..
    '\0' is called the null char.. every char array should have it at the end.. this is called null-termination
    C's Motto: who cares what it means? I just compile it!!

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Quote Originally Posted by gibsosmat View Post
    if this code is from a book.. you better change your book and start afresh

    to iterate a char array you will use a loop..
    if you know how it works.. then the condition for that loop should be to check that the char stored in the array is '\0' before it again enters into the loop..
    '\0' is called the null char.. every char array should have it at the end.. this is called null-termination
    no, this is code i wrote myself. Im just trying to understand the new information that is presented.
    mostly working with arrays and functions.
    ok, thanks!
    But i always thought that the null character /0 represented the end of the line, not just a space. Does it work both ways?
    hmm...
    actually, i can answer that myself with a web search.
    Thanks, Gibs!
    Last edited by MikeyIckey; 11-10-2007 at 10:51 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The end-of-string [which isn't quite the same as "end of line"] is a NUL('\0') (not to be confused with NULL which in C is used as a "pointer to nothing" value).

    A space is ' ' (or '\040' if you like, but most people will just be confused by that).

    However, the END-condition of a loop scanning for number of words is that it reaches a NUL-character.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    Quote Originally Posted by MikeyIckey View Post
    no, this is code i wrote myself. Im just trying to understand the new information that is presented.

    ok, thanks!
    But i always thought that the null character /0 represented the end of the line, not just a space. Does it work both ways?
    hmm...
    actually, i can answer that myself with a web search.
    Thanks, Gibs!
    end of the line is not '/0' and the slash you are using is also wrong. you need to use '\'
    if you want to check for end of the line (EOL) use '\n', its the same you use in printf()
    C's Motto: who cares what it means? I just compile it!!

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    oooh!
    ok!
    i'll try it out.
    Thanks guys!

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    done some modifications to the code.
    ive got this:
    Code:
    //program to count words in a user supplied string
    #include <stdio.h>
    
    char line [100];
    int wordcount(char line); 
    int numberOfWords = 0;
    
    
    int main()
    {
    	printf("Enter a line and I will count the number of words: ");
    	fgets(line, sizeof(line), stdin);
    	printf("There are %d words in the sentance.", wordcount(*line));	
    	
    }
    
    //function: wordcount
    //this function takes in the sentance variable, counts to each space character defined as ' ' and prints out the number of times 
    //it saw the ' ' (space) character.
    int wordcount(char line)
    {
    	int index = 0;
    	for(index <= sizeof(line), line == ' ', ++line)
    		{
    			++numberOfWords;
    		}
    	return numberOfWords;
    }
    but im getting an error during compilation.

    Code:
    C:\CODE>gcc -o wordcount wordcount.c
    wordcount.c: In function `wordcount':
    wordcount.c:23: error: syntax error before ')'
    does anyone see what the compiler talking about?

    thanks

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MikeyIckey View Post
    done some modifications to the code.
    ive got this:
    Code:
    //program to count words in a user supplied string
    #include <stdio.h>
    
    char line [100];
    int wordcount(char line); 
    int numberOfWords = 0;
    
    
    int main()
    {
    	printf("Enter a line and I will count the number of words: ");
    	fgets(line, sizeof(line), stdin);
    	printf("There are %d words in the sentance.", wordcount(*line));	
    	
    }
    
    //function: wordcount
    //this function takes in the sentance variable, counts to each space character defined as ' ' and prints out the number of times 
    //it saw the ' ' (space) character.
    int wordcount(char line)
    {
    	int index = 0;
    	for(; index <= sizeof(line), line == ' '; ++line)
    		{
    			++numberOfWords;
    		}
    	return numberOfWords;
    }
    but im getting an error during compilation.

    Code:
    C:\CODE>gcc -o wordcount wordcount.c
    wordcount.c: In function `wordcount':
    wordcount.c:23: error: syntax error before ')'
    does anyone see what the compiler talking about?

    thanks
    I'm thinking that's how you want it.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    ok, sorry, never mind, i figured it out.

    note to self: 'for' statements use semicolons and not commas.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    gah! beat me to it!

    XD
    thanks though

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    ok, i have one last question.

    ive modified the function again, but im getting another compiler error.

    error:
    Code:
    C:\CODE>gcc -o wordcount wordcount.c
    wordcount.c: In function `wordcount':
    wordcount.c:23: error: subscripted value is neither array nor pointer
    modified function:

    Code:
    int wordcount(char line)
    {
    	int index = 0;
    	for(; index <= sizeof(line), line[index] == '\0'; ++numberOfWords)
    		{
    			++index;
    		}
    	return numberOfWords;
    }

    im pretty sure i know what the error is talking about.
    its not happy with the 'line[index]' part, but im not too sure why.
    any help?
    thanks.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Line is not an array nor a pointer, as the error says. It's a single character.
    You might need char* line or char[] line.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    is it saying that because of how line is used in the function or how its declared globally?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You declared line as char. To the compiler, that means that it is NOT an array, Therefore, you can't use it as an array either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM