Thread: Word Length Statistics

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    3

    Word Length Statistics

    Hi guys. Can anyone help me to make that project.

    Write a program that calculates statistics on word length for a sentence. The sentence isterminated by a ’.’ For each found length of a word the number of words of that length
    is printed. Only those lengths that are found in the input are printed. Only letters from
    a-z or A-Z are allowed to form words. Words are separated by a space. No punctuation
    characters other than ’.’ are allowed. If any other input character is recognized or the
    input is longer than 80 characters the program displays ”NOT VALID” (see Hints). Note,
    that in the case that no word is present in the input, nothing is printed.
    % u6_stats
    Enter a sentence: Bolt was expected to use the super bark.
    Length 2: 1
    Length 3: 3
    Length 4: 2
    Length 5: 1
    Length 8: 1
    % u6_stats
    Enter a sentence: Something wasn’t right.
    NOT VALID
    Thats what i've already done.
    Code:
    #include <stdio.h>#include <conio.h>
    
    
    void main() {
    
    
    char s[100];
    int numOfWords, lengthOfWord = 0;
    int i = 0, j = 0;
    
    
    printf("Enter the text : ");
    fgets(s, sizeof(s), stdin);
    
    
    numOfWords = 1;
    
    
    while(s[i]!='\0'){
        if(s[i] == ' '){
            numOfWords++;
        }
        i++;
    }
    
    
    //printf("%d", numOfWords);
    
    
    i = 0;
    
    
    int help[numOfWords];
    int l;
    
    
    for(l = 0; l < numOfWords ; l++) help[l] = 0;
    
    
    while(s[i]!='\0') {
            if(s[i] != ' ' && s[i] !='\n' && s[i]!='.' ){
                lengthOfWord++;
                i++;
           }else{
               help[j] = lengthOfWord;
               j++;
               lengthOfWord = 0;
               i++;
            }
    }
    
    
    
    
    int repeat[80];
    for(l = 0; l < 80 ; l++) repeat[l] = 1;
    
    
    
    
    int num = 1;
    i=0,l=0;
    
    
    for(i = 0;i<numOfWords;i++){
     for(l=i+1;l<numOfWords;l++){
      if(help[i]==help[l]){                 
            repeat[l]='\0';
            num++;
            repeat[i] = num;
    
    
      }
     }
        num = 1;
    }
    
    
    l=0;
    for (l=0; l<numOfWords; l++)
    if (repeat[l]!='\0' && help[--l]!=help[++l]){
        printf("Length %d: %d\n", help[l],repeat[l]);
    }
     }
    So, the problem is that if i inpute a text like"abc abcd abc abc"
    the result will be like "length 3: 3 length 4: 1 length 3: 2".
    So when the program already compared 1st (here 3) element with otheres, this element will not be comparing again, but i have the 3d element with same length, and it that case, the program compare it with remained elements and print 2. How can i rework my code, if i dont wont to compare already compared elements.

    Second problem is that i need to get results from lowest length till highst.

    Please help me someone, and sry for my english.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I guess the first thing is work on the readability of your code.
    https://en.wikipedia.org/wiki/Indent_style

    Random indentation (or none at all) makes it really hard to follow the code.

    > if (repeat[l]!='\0' && help[--l]!=help[++l])
    You can be sure that this doesn't do what you think it should do.
    You have no idea how the --l and ++l interact - the C standard specifically states that the result is undefined behaviour.

    Be explicit and write something like
    if (repeat[l]!='\0' && help[l-1]!=help[l+1])
    or whatever it is you intended.

    But beware that +/-1 are array overruns at the limits of your for loop (first and last iterations).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by tobig View Post
    Second problem is that i need to get results from lowest length till highst.
    Focus on getting your statistics correct first without respecting the order. Afterwards you can sort the list as a second step from lowerst to highest. That way is probably the easiest approach.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  2. CPU Statistics
    By nascimento.rp in forum Linux Programming
    Replies: 4
    Last Post: 10-28-2006, 05:07 AM
  3. changing word length
    By mltngpot in forum C++ Programming
    Replies: 7
    Last Post: 01-21-2006, 09:55 AM
  4. Replies: 5
    Last Post: 09-28-2004, 12:38 PM
  5. FTP statistics
    By RoD in forum Tech Board
    Replies: 4
    Last Post: 05-29-2004, 09:51 AM

Tags for this Thread