Thread: program error :counting length of string

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    18

    program error :counting length of string

    Hi,
    I am a newbie and have been learning this language for only 3 months as a part of university degree. I can't figure out the problem with this code.

    It is basically a simple programme which tells a user how many letters he has entered. I have debugged the programme and it all works fine until it reaches the for loop inside string_length_func(string_data) this function. please help as i am stuck quite bad.

    cheers

    Code:
    #include <stdio.h>
    #define MAX_SIZE 10
    
    int string_length_func(char string_data[]); //func prorotype
    
    int main(void)
    {
    	/* Declare a character array to store the string in */
    	
    	char string_data[MAX_SIZE];
    	
            int length_word;
    	
    	/* Ask the user for a string */
    	printf("Enter a word: ");
    	scanf("%9s", string_data);
          
            /* calling a      function*/
           
     length_word = string_length_func(string_data);
    	
    printf("This word contains %d letters",length_word);
    
    int string_length_func(char string_data[])
    {
    	int element_num =0;
    
    /* here lies the problem as i have checked with debugger and all worked fine until here. */
           
    
    for (element_num = 0;((element_num < MAX_SIZE) &&    !(element_num == 0)) ; element_num ++)
    
    	if (string_data [0] == '\0')
    	printf("no letter was entered");
    
    
    return (element_num);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > printf("This word contains %d letters",length_word);
    > int string_length_func(char string_data[])
    You've started on your new funcion before you've finished main().
    From the look of things, a closing brace is all that is required.

    > !(element_num == 0)
    Why would you stop at the first character?
    Shouldn't you be testing string_data[element_num] == '\0' ?

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    18
    Code:
    for (element_num = 0;((element_num < MAX_SIZE) && !(element_num == '\0')) ; element_num ++)
    I have changed the for loop condition now...

    The output of this program without any error or warning is....

    Code:
    enter a word: four
    the word contains 0 letters //zero

    Now what should i do. Should it not say 4. i am so confused.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yeah, but what did you change it to?

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    18
    the condition in for loop now states !(element_num == '\0') whearas, before it was plain 0. but it is still not working. and no i am not missing any braces or whatever. its down to the problem with for loop. as it is not working as it should be.

    thanks salem

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    "Shouldn't you be testing string_data[element_num] == '\0' ?"
    Like I said, your loop always exists on the first iteration with your test. You need to test each character (not each index).

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    18
    thanks salem.
    i appreciate your help very much. the key was the word "character and index" in your post. I totally mixed them up.

    anyways... its working now
    bravo Salem. You are the man

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by bigjoke
    thanks salem.
    i appreciate your help very much. the key was the word "character and index" in your post. I totally mixed them up.

    anyways... its working now
    bravo Salem. You are the man
    Please get used to post the right solution after you fix it so al can check

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    18
    this is the working for loop. just replace it within original code and viola you have a program which counts the number of characters user has typed in


    Code:
    for (element_num = 0; 
    	(element_num < MAX_SIZE) && !(string_data[element_num] == '\0'); 
    	element_num ++);

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Presumably you ended main() and added a return statement too?

    And any part of a for loop can be empty, and since element_num is already 0, you can get rid of this:
    Code:
    for (element_num = 0;
    ->
    Code:
    for ( ;
    And you might find
    Code:
    string_data[element_num] != '\0'
    easier to read than
    Code:
    !(string_data[element_num] == '\0')
    This code
    Code:
    scanf("%9s", string_data);
    is making assumptions about the value of MAX_SIZE. I would either use
    Code:
    scanf("%*s", MAX_SIZE-1, string_data);
    or fgets()
    Code:
    fgets(string_data, MAX_SIZE, stdin);
    or, even better
    Code:
    fgets(string_data, sizeof(string_data), stdin);
    And don't forget to remove the newline:
    Code:
    #include <string.h>
    
    char *p;
    fgets(string_data, sizeof(string_data), stdin);
    if(p = strchr(string_data, '\n')) *p = 0;
    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
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    #include <string.h>
    
    char *p;
    fgets(string_data, sizeof(string_data), stdin);
    if((p = strchr(string_data, '\n'))!= NULL) {
     *p = 0;
    }

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by dwks
    This code
    Code:
    scanf("%9s", string_data);
    is making assumptions about the value of MAX_SIZE. I would either use
    Code:
    scanf("%*s", MAX_SIZE-1, string_data);
    Since this makes no sense, that'll leave us with fgets.
    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.*

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can't do that with scanf()? It works with printf().

    [edit]
    No, you're right: http://www.cplusplus.com/ref/cstdio/scanf.html

    How do you do it with scanf(), then?
    [/edit]
    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.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by dwks
    How do you do it with scanf(), then?
    I believe the general form of the hack is like this.
    Code:
       char format[20];
       sprintf(format, "%%%lus", (long unsigned)(sizeof format - 1));
       scanf(format, string_data);
    But then that has a similar issue. You can work around it, but this just seems to make a greater case for using fgets.
    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.*

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, fgets() it is, then.
    Code:
    if((p = strchr(string_data, '\n'))!= NULL) {
    What's wrong with mine?
    Code:
    if(p = strchr(string_data, '\n'))
    Should it be like this?
    Code:
    if((p = strchr(string_data, '\n')))
    But that doesn't make sense -- the FAQ uses this:
    Code:
    while(file = readdir(dir))
    [edit]
    No, it doesn't. [edit=2] The FAQ, I mean. [/edit] So it probably does need the extra ()'s, right?

    It needs the ()'s to compile with TC 2.01, but of course that means nothing.
    [/edit]
    Last edited by dwks; 12-27-2005 at 03:06 PM.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Help with string program
    By Duskan in forum C Programming
    Replies: 8
    Last Post: 04-02-2007, 08:27 AM
  4. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM