Thread: Help

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    19

    Help

    i want to make a program in c :
    but am a bit stuck, so can you anyone jst guide me through it.
    Basically the program has to read in a whole paragraph, and the the program will have to find the words in the paragraph, then it has to sort the words into alphabetically order and and also count the number of times the word appeared in the paragraph and then print, the words in alphabetically order with the frequency.
    cheers , if anyone can just guide me through it it would be good. Ps i have developed the sorting algorithm for the program... its an insertion sort.

  2. #2
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    What are you stuck on?

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    • You probably don't need to read in a whole paragraph. Just read lines.
    • Then separate the lines into words, with strtok() (ugh) or some function of your own. You may find isspace() or isalpha() from <ctype.h> useful.
    • Note that you might want to discard non-alphanumeric characters, so that "this." and "this" and "this," are the same word.
    • Next you have to save each word. If it already exists, increment the counter for it; otherwise, add it in. You could use something like this to store each word:
      Code:
      struct word_t {
          char *word;  /* malloc()'d space for the word */
          int count;  /* number of times the word occurs */
      };
      Or you might want an array if you're not familiar with malloc().
    • Note that you'll need to use strcmp() to compare the words. If you want case-insensitive comparison (i.e., "a" == "A"), you could use the non-standard strncmp(), but you'd probably be better off writing your own. tolower()/toupper() from <ctype.h> will help you with this.
    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.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    19
    hi
    here is my code for scan function;
    how can i make the function store the words, into my program.
    Code:
     
    #include<stdio.h>
    #include<ctype.h>
    #define MAXWORDS 20
    #define MAXLETTERS 20 
    int main(int argc,char *argv[]){
       int character=0;
       int flag=0;
       while(((character=getchar())!=EOF)){
          if (ispunct(character) && flag==0){
             printf("\n");
             flag=1;
          }
          if (isspace(character) && flag==0){
             printf("\n");
             flag=1;
          }
          if (isalpha(character)){
             putchar(character);
             flag=0;
          }
          
       }
       return 0;
    }

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    "flag" is a pretty bad variable name in this case. Perhaps "printed_newline" would be better.

    How can you store the words instead of printing them? Well, declare an array to store the data in. If you're not feeling up to allocating space and whatnot, then perhaps something like this.
    Code:
    char word[MAXWORDS][MAXLETTERS];
    Then, in order to use your existing code, you'll need a counter for which word you're storing and a counter for which character in that word you're dealing with. (Or just keep the word always NULL-terminated.)

    BTW, there are characters that return false for ispunct(), isspace(), and isalpha(). Numbers, for example. I'd use isspace() and !isspace().
    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.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Lol, I believe this is the notorious exercise #153 in "The C book"

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    19
    hey guys need some help any one willing 2 help me, here is my separating function, but i can't include a apostrophe in it, any one willing to help me out. cheers.
    Code:
    int is_separator(char ch) {
      /* Separators are space, comma, colon, semicolon, double quote, question mark, exclamation, and period */
      static char separators[] = { ' ' , ',',':' , '\"', '?' , '!' , '.'};
      int i = 0;
    
      for(i = 0 ; i<sizeof separators ; i++)
      {
        if(ch == separators[i])
          return TRUE;
      }
    
      return FALSE;
    }

  8. #8
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Why can't you include an apostrophe?

    I'd suggest a switch statement rather than a loop

    Code:
    int is_separator(char ch){
     switch(ch){
      case ' ': case ',': case ':': case '\"': case '?': case '!': case '.': case '\'':
       return 1;
      default:
       return 0;
     }
    }

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I wouldn't suggest either

    Code:
    int is_separator(char ch)
    {
        static const char seps[] = " ,:\"?!.'";
        return (strchr(seps, ch) != NULL);
    }

  10. #10
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Not that it really matters for a small program like this, but a quick benchmark says that strchr is ~50&#37; slower than the switch.

    So there

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    So why not table access?

    Code:
    int is_separator(char ch)
    {
      static char T[256] = { -1 };
      if (T[0] == -1)
      {
          memset(T, 0, sizeof(T));
          T[' '] = 1;
          T[','] = 1;
          T[':'] = 1;
          T['?'] = 1;
          T['!'] = 1;
          T['.'] = 1;
          T['\''] = 1;
      }
    
      return(T[ch]);
    }
    Just a concept, didn't test...

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    19
    Quote Originally Posted by thamiz View Post
    hi
    here is my code for scan function;
    how can i make the function store the words, into my program.
    Code:
     
    #include<stdio.h>
    #include<ctype.h>
    #define MAXWORDS 20
    #define MAXLETTERS 20 
    int main(int argc,char *argv[]){
       int character=0;
       int flag=0;
       while(((character=getchar())!=EOF)){
          if (ispunct(character) && flag==0){
             printf("\n");
             flag=1;
          }
          if (isspace(character) && flag==0){
             printf("\n");
             flag=1;
          }
          if (isalpha(character)){
             putchar(character);
             flag=0;
          }
          
       }
       return 0;
    }
    would i be able to use the code above, to my my is_separator, because i need to separate the words based on the above characters...
    if i input any words with punctions the function has to split when there is a puctation marks
    for example if i input: the!!! didn't ?
    the out put should be: the
    didn
    t
    etc...
    any one can help me merge the two function i wrote together

Popular pages Recent additions subscribe to a feed