Thread: Homework help.

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    Homework help.

    Hello.

    I have an assignment asking me to read a chunk of text from a file and report how many words, characters, sentences, and the longest word. Now I got the first three but am stuck with the last one.

    What I think has to be done is to split the sentences so that each word becomes a string and then compare them. I am not quite sure how to split a sentence into strings though.

    If any of you could help or point me in the right direction, it would be greatly appreciated. Also, if there is another more simpler way to do this, please let me know.

    Here is my assignment so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    int
    
    wordcount (char string[])
    {
    int words=0, i;
    for (i=0; i<strlen(string); ++i)
    if (string[i]==' ')
    words = words + 1;
    return (words);
    }
    int
    charcount (char string2[])
    {
    int chars=0, i;
    for (i=0; i<strlen(string2); ++i)
    if (string2[i]!='\0')
    chars = chars + 1;
    return (chars);
    }
    
    int
    sentence_count (char string3[])
    {
    int sentences=0, i;
    for (i=0; i<strlen(string3); ++i)
    if (string3[i]=='.'&& string3[i+2]>'A' && string3[i+2]<'Z')
    sentences = sentences + 1;
    return (sentences);
    }
     
     int main (void)
     
     {
      char s[2000];
      int wordnum, i, charnum, sentnum, j;
      FILE *input;
     
     
      input = fopen("phrase.txt", "r");
      fgets (s, sizeof(s), input);
     printf ("The sentence is:\n\"%s\"\n\n\n", s);
      
      
      wordnum = wordcount (s);
      ++wordnum;
     printf ("The sentence contains %d words.\n", wordnum);
     
    charnum = charcount (s);
     
      printf ("The sentence contains %d characters.\n", charnum+1);
      
       sentnum = sentence_count (s);
    printf ("There are %d sentences.\n", sentnum+1);
    
    ;  
     return (0);
    }
    Thank You.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Here's a simpler idea - for every word, count the number of letters it has in it. You're program is walking over them anyway, so it's no big deal.

    Keep a wordlen and maxwordlen variable. If wordlen > maxwordlen, then maxwordlen = wordlen.

    Be sure to initialize maxwordlen to 0 at the start of the program.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    3
    ok i think i did that and I am now able to display the amount of characters the longest word has. Now my problem is displaying that word on the screen. I may be overcomplicating things again, but what I am thinking is to scan each letter into an array during the loop and then after each space rescan over that array if the word is longer. The rest of the array is filled with spaces to avoid random garbage.

    Is this even possible to do, or is there a simpler way? Again any help and a point in the right direction will be greatly appreciated. Thank you.

    This is my new code:

    Code:
    #include <stdio.h>
    #include <string.h>
    int
    wordcount (char string[])
    {
    int words=0, i;
    for (i=0; i<strlen(string); ++i)
    if (string[i]==' ')
    words = words + 1;
    return (words);
    }
    int
    charcount (char string2[])
    {
    int chars=0, i;
    for (i=0; i<strlen(string2); ++i)
    if (string2[i]!='\0')
    chars = chars + 1;
    return (chars);
    }
    
    int
    sentence_count (char string3[])
    {
    int sentences=0, i;
    for (i=0; i<strlen(string3); ++i)
    if (string3[i]=='.'&& string3[i+2]>'A' && string3[i+2]<'Z')
    sentences = sentences + 1;
    return (sentences);
    }
     
     int main (void)
     
     {
      char s[2000];
      int wordnum, i, charnum, sentnum, j, letternum=0, max=0;
      FILE *input;
     
     
      input = fopen("phrase.txt", "r");
      fgets (s, sizeof(s), input);
     printf ("The sentence is:\n\"%s\"\n\n\n", s);
      
      
      wordnum = wordcount (s);
      ++wordnum;
     printf ("The sentence contains %d words.\n", wordnum);
     
    charnum = charcount (s);
     
      printf ("The sentence contains %d characters.\n", charnum-1);
      
       sentnum = sentence_count (s);
    printf ("There are %d sentences.\n", sentnum+1);
    
    for (i=0; i<strlen(s); ++i)
    if (s[i]>='A'&& s[i]<='Z'||
    s[i]>='a'&&s[i]<='z')
    {
    letternum = letternum + 1;
    }
    
    else if(letternum>=max)
    	{
    	max=letternum;
    	letternum=0;
    	}
    else
    {
    letternum = 0;
    }
    
    printf ("The longest word has %d letter.\n", max);
    
    
     return (0);
    }

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Try going through every possible word, and use strlen() to check how long the word is, if it is == maxwordlen, then that must be your word (or one of them at least).

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    The standard library contains a so called tokenizer:

    Code:
    #include <string.h>
    char *strtok( char *str1, const char *str2 );
    Google for strtok() and you will find lots of documentation.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't rescan. Just save the longest word you've found so far, in a small char array using memcpy() or whatever you like.

    The max word will change in the array, as longer words are found. After one scan, you'll have the longest word. Add an ending end of string char onto it, and you can use a simple printf("%s", longestword) to display it.

    Unless it's entirely trivial, you never want to rescan, if it can be avoided.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Start using indentation, please. This code is a mess and a horror on everyones' eyes, including your own. Indentation is an extremely important concept. Learn it, and learn it well. And learn it early.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    3
    Firstly, I apoligize for making everyone read my un-indented mess. I wasnt indenting because i was more worried about my code.
    Secondly, I am done.
    And thirdly, I want to thank you guys for taking the time to help me. I really appreciate it. You guys are great.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  5. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM