954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help with programming

Hi guys I am really having a hard time with C programming. Our lectures are very fast and not beneficial to those with non-C backgrounds. We were required to write a code for counting frequency of words from stdin and then listing them in descending order with those with equal length being alphabetized. Please help me because I am really lost with this program. I really need it.

bobcats
Newbie Poster
11 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Read in user input
http://www.daniweb.com/tutorials/tutorial45806.html

Then use the space as a delimiter to separate the sentence into words.

http://www.daniweb.com/code/snippet443.html

Once you have your array of words find out their length, possibly using strlen().

To sort those words of the same length alphabetically, use strcmp()

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Thanks for the links sir. Its just that I'm going nothing with my code. I posted what I have done below and its a wreck. I can't even convert uppercase input to lower case. How to count the occurrences of a word is a big puzzle also for me. I wish you could help me.

#include<stdio.h>
#include<string.h>

int main (void)
{
        char haystack[1028];
        char needle[] = " a";
        char c = 'a';
        char *ph;
        fgets(haystack,sizeof(haystack),stdin);
        while(c<='z'){
           ph = haystack;
           if(c==*ph) {
              putchar(*ph++);
              while(!isspace(*ph)){ 
              putchar(*ph++); 
              printf("\n");
           }
           while( ph=strstr(ph,needle) ){
              putchar(*ph++);
              while(!isspace(*ph)) { 
              putchar(*ph++); 
              printf("\n");             
              }
           }
        needle[1]=++c;
        }
puts("");
return(0);
}
bobcats
Newbie Poster
11 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Occurrences can be done with what's called a frequency table. It's a really cool thing, and it's really easy to implement. But, I'm not going to write your program for you, so I'll give you a frequency table that counts the occurrences of single characters and let you figure out how to do it with words using the excellent links you've already been given (otherwise I would hurt Dave's feelings):

#include <stdio.h>
#include <limits.h>
#include <ctype.h>

int main ( void )
{
  int freq[CHAR_MAX + 1] = {0};
  int i;
  char input;

  while ( ( input = getchar() ) != EOF )
    ++freq[input];

  for ( i = 0; i < CHAR_MAX + 1; i++ ) {
    if ( freq[i] != 0 ) {
      if ( isalpha ( i ) )
        printf ( "%c:\t%d\n", i, freq[i] );
      else
        printf ( "%#X:\t%d\n", i, freq[i] );
    }
  }

  getchar();

  return 0;
}

I talk about frequency tables a lot because they're so useful, you might even be able to find an exact solution to your problem by searching the forum. ;)

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

sir thanks for the things about frequency tables but we arent allowed to use them. here's what my prof wants.

Input. Hello world! Hello country. Hello world! Hello card. Hello apple.
Output:
hello 5
world 2
apple 1
card 1
country 1


I'm really having difficulty with this program. Hope anyone could help.

bobcats
Newbie Poster
11 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

>madam thanks for the things
Fixed quote ;)

>but we arent allowed to use them.
How else does your professor expect you to keep track of the number of times a word is present in a sentance, then? I'll let Narue answer this one...

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

>thanks for the things about frequency tables but we arent allowed to use them.
What are you allowed to use if you can't cache the words and counts? I can think of one way (that could be considered a beginner solution) off the top of my head, and the logic is extremely painful even for experienced programmers.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

> thanks for the things about frequency tables but we arent allowed to use them.
Which is why it is pointless to ask people to do your homework.

Not only would we have to vaguely guess at what you really wanted, we also have to guess at how much to dumb it down to the point where you might have plausibly written it. This just isn't going to happen.

> Our lectures are very fast and not beneficial to those with non-C backgrounds
Perhaps you're on the wrong course then. You (as a group of non-C people) need to go talk to the lecturers to voice your concerns.

If the course material said "assumes familiarity with C", then your bluff has been called. Quit now and get onto another course before it's too late to catch up with either of them.

This is going to get a lot harder very quickly, so you need to look at the long term problem as well. Us providing you with one ready made answer is not going to make you an instant C expert. All it will do is keep you on the course for another week until the next (and even more unobtainable) assignment is handed out.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

http://www.java2s.com/Code/C/Structure/Usingastructuretorecordthecountofoccurrencesofaword.htm

I've not tested it because I don't have a compiler to hand.
But using that as a guideline it might get you half way there.

But you also have to sort it by occurrence, and those words that are of the same occurrence must be sorted in alphabetical order.

In that case you'll prolly need to define your own sorting routine from the struct.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

um sorry for the confusion. What I meant with the things not being allowed is that the output wanted was to be straightforward. I am thinking with the frequency table is how I can account for a very long string. I'm really sorry if I offended anyone but I don't know where to start. Im puzzled with these.

bobcats
Newbie Poster
11 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Hello I was thinking about the sorting part. Like I said I don't have my compiler so this is what I rolled out from the top of my head, I'm not sure if it works?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NELEMS 5
//could be done dynamically with malloc perhaps?

struct crap
{
   char word[1024];
   int wordOccurrence;
};

static int wordOccurrenceSort(const void *, const void *);

int main()
{
   size_t i;
   static struct crap stuff[NELEMS] =
     
     {
      {"card",1},
      {"Hello",5},
      {"apple",1},
      {"country",1},
      {"world",2}
     };

   
   qsort(stuff, NELEMS, sizeof stuff[0],wordOccurrenceSort);

   puts("Sort by word occurence:\n");
   
   for (i = 0; i < NELEMS; ++i )
   {
      printf("\n%-20s %2d",stuff[i].word, stuff[i].wordOccurrence);
   }
        
   getchar();
      
   return 0;
}


static int wordOccurrenceSort(const void *p1, const void *p2)
{
   struct crap *sp1 = (struct crap *) p1;
   struct crap *sp2 = (struct crap *) p2;
   
   if (sp2->wordOccurrence > sp1->wordOccurrence )
   {
      return 1; //not sure what value to assign?
   }
   
   if (sp2->wordOccurrence < sp1->wordOccurrence )
   {
      return 0;//not sure what value to assign?
   }
   
   if ( sp2->wordOccurrence == sp1->wordOccurrence)
   {
      int order = strcmp(sp1->word,sp2->word);
      return order;
     //not sure what value to assign?
   }
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
static int wordOccurrenceSort(const void *p1, const void *p2)
{
   struct crap *sp1 = (struct crap *) p1;
   struct crap *sp2 = (struct crap *) p2;
   
   if (sp2->wordOccurrence > sp1->wordOccurrence )
   {
      return 1; //not sure what value to assign?
   }
   
   if (sp2->wordOccurrence < sp1->wordOccurrence )
   {
      return 0;//not sure what value to assign?
   }
   
   if ( sp2->wordOccurrence == sp1->wordOccurrence)
   {
      int order = strcmp(sp1->word,sp2->word);
      return order;
     //not sure what value to assign?
   }
}

You could simply have done something like this since this function is required to return how one element is placed wrt another and not any absolute value. Only the sign matters here (+ve, -ve, zero).

static int wordOccurrenceSort(const void *p1, const void *p2)
{
   struct crap *sp1 = (struct crap *) p1;
   struct crap *sp2 = (struct crap *) p2;
   
   return  (sp1->wordOccurrence - sp2->wordOccurrence) ;
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

You could simply have done something like this since this function is required to return how one element is placed wrt another and not any absolute value. Only the sign matters here (+ve, -ve, zero).

static int wordOccurrenceSort(const void *p1, const void *p2)
{
   struct crap *sp1 = (struct crap *) p1;
   struct crap *sp2 = (struct crap *) p2;
   
   return  (sp1->wordOccurrence - sp2->wordOccurrence) ;
}

Maybe, like I said I don't have my compiler to test it. I wrote that on the fly.

How does your version sort the words of same occurrence into alphabetical order though. I can't see it?We were required to write a code for counting frequency of words from stdin and then listing them in descending order with those with equal length being alphabetized

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
How does your version sort the words of same occurrence into alphabetical order though. I can't see it?


Oh, I guess I overlooked that requirement. Here is the fix:

static int wordOccurrenceSort(const void *p1, const void *p2)
{
   struct crap *sp1 = (struct crap *) p1;
   struct crap *sp2 = (struct crap *) p2;

    if (sp1->wordOccurrence == sp2->wordOccurrence)
        return strcmp (sp1->word, sp2->word) ;
    else
        return  (sp1->wordOccurrence - sp2->wordOccurrence) ;
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Don't forget he/she wants it in descending order?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Well, doing that would be really simple. Posting the entire solution would be like wasting the efforts Narue and the others have put in solving the OP's problem.

I would leave the OP to figure out this one for himself.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

>Well, doing that would be really simple.

No I wasn't asking, I was correcting. :cheesy:

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
No I wasn't asking, I was correcting. :cheesy:


;)

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

>What I meant with the things not being allowed is that the output wanted was to be straightforward.
The output has no relationship to how you produce the answer. You can solve the problem a dozen wildly different ways and get exactly the same desired output that you posted.

>Only the sign matters here (+ve, -ve, zero).
>return (sp1->wordOccurrence - sp2->wordOccurrence) ;
Sure, only the sign matters, but it's also the sign that kills you with this code. You're a smart guy, so I'll give you three words of advice on why this is a poor solution: signed integer overflow.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

>You're a smart guy, so I'll give you three words of advice on why this is a poor solution: signed integer overflow.

He he, does that mean my code is safer? To be honest I don't know? This is just guess work - but it would be nice to know why. How long are you on maternity leave for?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You