Thread: length of string etc.

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    Cool length of string etc.

    program must let user input words
    stops when length of word is 4 characters
    keeps track of length of all words, prints out shortest and longest words
    (part of the instructions say that the program will determine which words would come first and last if were listed in dictionary order - though I don't understand what that has to do with length of word)

    Can someone get me on the right path to the order of the functions called, and maybe help with the code that checks length and stores smallest so far in the smaller_word string and stores the longest word so far in the longest_word string?????

    Code:
    
    
    #include <stdio.h>
    #include <string.h>
     
    #define WORD_LEN 20
     
    read_line(char str[], int );                  /*  function prototype   */
    size_t strlen(const char *);                /*  function prototype   */
     
    main()
     
    {  
        char word[WORD_LEN + 1];            /*  array/string for words input by user  */
        char *ptr;                                    /*  char pointer for func strlen      */
        char smallest_word[WORD_LEN +1];  /*  smallest word in length            */
        char largest_word[WORD_LEN +1];   /*  largest word in length            */
        char fword[WORD_LEN +1];
        char lword[WORD_LEN +1];    
        int n;                                          /*  counter variable */
     
        
    while (strlen(word) != 4)         /*   this is not setup right, not sure if while or for loop best */
          {
              printf("Enter word: \t\n");
              read_line(str[], strlen(word));  /*  call function to read word char by char */
              puts (word);                         /*  print word              */
              strlen(word);                        /*  call function to find string length  */
                                   /*  loop to keep track of smallest and longest word here */
           
          }
        
         if (strlen(word) == 4)         /*  somewhere this fits in to stop if 4 letter word is input */ 
             break;
    
    
    
     
       printf("First word alphabetically :  %s\n", fword);     /*   printing */
       printf("Last word alphabetically :  %s\n",  lword);     /*  all the   */
       printf("Smallest word :  %s\n", smallest_word);        /*  final      */
       printf("Longest word :   %s\n", longest_word);        /*  output   */
     
    return 0;
    }
     
    read_line(char str[], int n)         /*  function to read word input   */
                                                     /*    character by  character      */
    {
        char ch;
        int i=0; 
        while ((ch = getchar() != '\n')
             if (i < n )
                  str[i++] = ch;
             str[i] = '\0';                     /*  terminates string  */
             return i;                          /*  returns # of characters  */
    }
     
    size_t strlen(const char *s)       /*  function to keep track of length */
    {                                               /*  of each word input                  */
       size_t n=0;
       while (*s++)
          n++:
       return n;                            /*  returns length of string   */           
    }

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    why did you declare and define strlen() in your program. That exact same function with the exact same parameters and returning the exact same thing is IN string.h.
    My Website

    "Circular logic is good because it is."

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    23
    why did you declare and define strlen() in your program

    -----------------------

    I suppose cause I didn't understand why my book had the function written out longhand, and didn't know what the strlen function is as it is in string.h library.

    so I don't need the code

    Code:
    size_t strlen(const char *s)       /*  function to keep track of length */
    {                                               /*  of each word input                  */
       size_t n=0;
       while (*s++)
          n++:
       return n;                            /*  returns length of string   */           
    }
    i just type strlen(string name here) and that does the work???

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    Question Getting past the strlen issue ..........

    Can someone reply with an answer to my original problem???

    I need to understand the order of the function calls AND the loop involved to find the smallest to largest length string.

    Got any ideas??

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi,
    Apart from the use of strlen() when it is already defined in library <string.h>...here are your other faults..
    Code:
    /*in read line their is no return type 
    and int has no identifier name*/
    read_line(char str[], int );                  /*  in correct function prototype   */
    size_t strlen(const char *);                /*  function prototype   */
    incorrect way of coding prototypes
    correct way
    Code:
    <return type> function name (<parameters>)
    /*so*/
    void read_line (char str[], int num);
    in fact from here on in your mistakes are numerous, I suggest you think again. Put through a compiler and that should pick up most mistakes..although not all..sorry
    Last edited by bigtamscot; 09-27-2001 at 05:39 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    Unhappy

    ok fixed the prototype problems

    now I just need a little guidance in the loop needed to sort the strings input (words) into smallest to largest lengths. How do I do that?

    What is the best way to write the while loop that needs to stop if a word of exactly 4 characters is input????
    eg. if (strlen == 4)
    break;

    And what would be the point to knowing the alphabetical order of words input when looking for smallest to largest strings???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating the length of a string
    By BSmith4740 in forum C Programming
    Replies: 32
    Last Post: 07-02-2008, 12:51 PM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  3. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM
  4. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM