I'm trying to count the frequency of the phrase "ing"

This is a discussion on I'm trying to count the frequency of the phrase "ing" within the C Programming forums, part of the General Programming Boards category; If anyone can take a couple minutes to help me out it would be greatly appreciated. Right now, the counters ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    7

    I'm trying to count the frequency of the phrase "ing"

    If anyone can take a couple minutes to help me out it would be greatly appreciated. Right now, the counters for the frequency of the phrase "ing" in the beginning, middle, and end of a word are well into the thousands, even if "ing" isn't even present in the input. It might not be that big of a problem, I'm just really new to this and trying to figure it out.
    Code:
      
     char current,char1,char2,char3,char4,char5;
     int anum,num,letter,wcount,starting,ending,middle,ingcount,anum3count,prevalue;   //   variable types
     anum = 0;
     num = 0;      // variable values
     letter = 0;
     wcount = 1;
     char1 = '#';
     char2 = '#';   // set all char variables to '#' so that they fill in one at a time
     char3 = '#';
     char4 = '#';
     char5 = '#';
     printf("Enter text ending with a '#' symbol. \n");
     scanf("%c",&current);   //reads the first one
     if (current == ' ')
        prevalue = current;
        
        
     while (current != '#')      //one while loop to test for everything
     {
       if ((current >= '.') && (current <= '~'))      //searches alphanumeric characters
         anum +=1;   
       if ((current >='0') && (current <='9'))      // if the ASCII code signifies a number
         num +=1;
       if ((current >= 'A') && (current <= 'z'))    // if the ASCII code signifies a letter
         letter += 1;     // adds one letter
         
       if (char1 == '#')
         char1 = current;
         else
             if (char2 == '#')
                char2 = current;
                else
                    if (char3 == '#')
                       char3 = current;
                       else
                           if (char4 == '#')
                              char4 = current;
                              else
                                  if (char5 == '#')
                                     char5 = current;
                      
       if ((char1 == 'i') && (char2 == 'n') && (char3 == 'g'))   //searches for frequency of 'ing', stores in 'ingcount'
          ingcount += 1;
       if ((char1 == ' ') && (char2 == 'i') && (char3 == 'n') && (char4 == 'g'))  //searches for words that start with 'ing', stores in 'starting'
          starting += 1;
       if ((char1 == 'i') && (char2 == 'n') && (char3 == 'g') && (char4 == ' '))   //searches for words that end with 'ing', stores in 'ending'
          ending += 1;
       if ((char1 != ' ') && (char2 == 'i') && (char3 == 'n') && (char4 == 'g') && (char5 != ' '))   //searches for words that have 'ing' in them, stores in 'middle'
          middle += 1;     
       if ((char1 >= '.') && (char1 <= '~'))
          if ((char2 >= '.') && (char2 <= '~'))       //searches for three alphanumerics in a row
             if ((char3 >= '.') && (char3 <= '~'))
                anum3count += 1;        //if it finds a set of three, it adds one to the counter
       char1 = char2;
       char2 = char3;             //shifts the values of the variables for the next loop
       char3 = char4;
       char4 = char5;
         
       scanf("%c",&current);      //reads the next one
       if (char5 != '#')
          char5 = current;    //sets char5 to the new variable since the rest were moved back one.
       if ((prevalue == ' ') && (current != ' '))
          wcount += 1;    //checks to see if there is a space and then a non-space character (start of a new word)
       if (current == ' ')
          prevalue = current;    // if current is a space

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    For clarity sake, The purpose of the program is to count the amount of alphanumeric characters relative to the amount of numbers and letters, count the amount of words, and count the presence of the phrase "ing", in addition to the location of the phrase (at the beginning, middle, or end of the word).

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Before you start adding to ingcount, shouldn't you start it at 0?

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,041
    You don't initialize ingcount. Set it to 0 before you use it.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    That was a stupid mistake, but somehow it doesn't seem to make much of a difference. The addition of ingcount = 0; doesn't really seem to change anything. There are no syntax errors, I don't think. I think something in this area of the code is preventing it from working:

    Code:
       if (char1 == '#')
         char1 = current;
         else
             if (char2 == '#')
                char2 = current;
                else
                    if (char3 == '#')
                       char3 = current;
                       else
                           if (char4 == '#')
                              char4 = current;
                              else
                                  if (char5 == '#')
                                     char5 = current;
                      
       if ((char1 == 'i') && (char2 == 'n') && (char3 == 'g'))   //searches for frequency of 'ing', stores in 'ingcount'
          ingcount += 1;
       if ((char1 == ' ') && (char2 == 'i') && (char3 == 'n') && (char4 == 'g'))  //searches for words that start with 'ing', stores in 'starting'
          starting += 1;
       if ((char1 == 'i') && (char2 == 'n') && (char3 == 'g') && (char4 == ' '))   //searches for words that end with 'ing', stores in 'ending'
          ending += 1;
       if ((char1 != ' ') && (char2 == 'i') && (char3 == 'n') && (char4 == 'g') && (char5 != ' '))   //searches for words that have 'ing' in them, stores in 'middle'
          middle += 1;     
       if ((char1 >= '.') && (char1 <= '~'))
          if ((char2 >= '.') && (char2 <= '~'))       //searches for three alphanumerics in a row
             if ((char3 >= '.') && (char3 <= '~'))
                anum3count += 1;        //if it finds a set of three, it adds one to the counter
       char1 = char2;
       char2 = char3;             //shifts the values of the variables for the next loop
       char3 = char4;
       char4 = char5;
         
       scanf("%c",&current);      //reads the next one
       if (char5 != '#')
          char5 = current;    //sets char5 to the new variable since the rest were moved back one.
    Everything else seems to be working okay. If I put as an input, for example, the word "Hello", then It'll output all the correct information except for the frequency of the 'ing' phrase. No matter what input I enter, it always says starting = 8, ending = 4 million something, and middle = 4 million something. (slightly more). These values never change no matter what the input. Any ideas?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You do have to pay a little bit of attention -- all your counters need to start at 0 (ingcount is merely one of them).

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Thanks...that did help. Or at least, it fixed the random assignments. The algorithm that I have is still not working. Now all the counts stay at zero for the "ing" frequency.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Now we get to move on a little lower:
    Code:
    char1 = char2;
       char2 = char3;             //shifts the values of the variables for the next loop
       char3 = char4;
       char4 = char5;
    So we read in one character -- we put it in char1. Then we get here; char1 vanishes in a puff of logic and we're back to five # signs again. You can't shift until you've read five characters in.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    I already made a change to that, sorry I didn't post it yet. Would this do the trick?

    Code:
       if ((char1 == 'i') && (char2 == 'n') && (char3 == 'g'))   //searches for frequency of 'ing', stores in 'ingcount'
          ingcount += 1;
       if ((char1 == ' ') && (char2 == 'i') && (char3 == 'n') && (char4 == 'g'))  //searches for words that start with 'ing', stores in 'starting'
          starting += 1;
       if ((char1 == 'i') && (char2 == 'n') && (char3 == 'g') && (char4 == ' '))   //searches for words that end with 'ing', stores in 'ending'
          ending += 1;
       if ((char1 != ' ') && (char2 == 'i') && (char3 == 'n') && (char4 == 'g') && (char5 != ' '))   //searches for words that have 'ing' in them, stores in 'middle'
          middle += 1;     
       if ((char1 >= '.') && (char1 <= '~'))
          if ((char2 >= '.') && (char2 <= '~'))       //searches for three alphanumerics in a row
             if ((char3 >= '.') && (char3 <= '~'))
                anum3count += 1;        //if it finds a set of three, it adds one to the counter
       if ((char1 != '#') && (char2 != '#') && (char3 != '#') && (char4 != '#') && (char5 != '#'))
          char1 = char2;
          char2 = char3;             //shifts the values of the variables for the next loop
          char3 = char4;
          char4 = char5;
    Everything underneath stays the same.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Even after changing that, I'm still getting the same outputs.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
      if ((char1 != '#') && (char2 != '#') && (char3 != '#') && (char4 != '#') && (char5 != '#'))
      {
          char1 = char2;
          char2 = char3;             //shifts the values of the variables for the next loop
          char3 = char4;
          char4 = char5;
      }
    If you want to see what's going on with these char variables, you can always print them out in advantageous places.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    When I tried to print char1 directly after its assignment, it said 'iii'. If I print all of them after their first assignments, it says a bunch of gibberish more or less. I'm really completely lost now. Retracing my steps I honestly have no idea what's going on.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am not even reading anything other than the OP code which is kind of nasty.

    Why note do something like:

    Example:
    Code:
    #include <string.h>
    #include <ctype.h>
    
    int count_suffix(const char *s, const char *target)
    {
      int c = 0;
      size_t length = strlen(s);
      const char *pos = target, *end = target + strlen(target);
    
      for(pos = strstr(pos); pos && pos < end; pos = strstr(pos+1))
        if(!isalnum(pos[length]))
          ++c; // this only adds to the count if the word actually ends in the suffix
    
      return c;
    }
    
    // Now you can easily find the "ing" ending (or any other suffix for that matter).
    int main(void)
    {
      printf("&#37;d \"ing\" endings found in the string.", count_suffix("ing", "I am having a great day today. Wouldn't you also find giving a handful of people who are fishing a tap dancing coupon a great thing to do, too?"));
    
      return 0;
    }
    Yeah... I know its not the best. But it works.

  14. #14
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    You're in a classic situation where the code you've written is too inefficient, badly written and hard to read for you to continue with it, in my opinion. You made bad code, it didn't work, so you wrote more bad code to correct it. Now you've got a lot of bad code with bad corrections, and I highly recommend you google "string.h functions" and consider another way to do this - look at master5001's post.

    That may be too advanced for you, but you should be able to do something similar. Try writing an algorithm down on paper - basically the steps you'd take to methodically and algorithmically count the number of ings - and try implementing that in your program.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well sometimes giving code that is obviously too advanced for the OP is not a bad way to answer the question. It demonstrates that cleaner looking code is possible. It also produces code that an instructor knows the person did not write themself.

    Pointers are very useful for this type of application. Read up on them, and use them. If you are not on that topic yet in class, then I recommend using indices of an array instead. You should not be copying over data so much just to do a simple comparison. Think of it like wanting to find a shirt exactly like the one you have on. So to do it, you go buy the same shirt again, then take the shirt you just bought from Macey's over to Wal-mart and asking "Do you have one like this one?" Instead of just pointing to your original shirt and asking the incompetent boob at Wal-mart "Do you have one like this?"

    You should not need to copy stuff over is the point. Just use the original.

    Example:
    Code:
    if((array[c] == 'i') && (array[c+1] == 'n') && (array[c+2] == 'g'))

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count and print the frequency of each ASCII character
    By s_jsstevens in forum C Programming
    Replies: 3
    Last Post: 02-07-2011, 08:33 PM
  2. Need some guidance.
    By Kinto in forum C Programming
    Replies: 10
    Last Post: 05-31-2009, 12:02 AM
  3. bintree and count (withouth using template)?
    By cubimongoloid in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2009, 06:22 AM
  4. input question
    By piyush_v in forum C Programming
    Replies: 9
    Last Post: 04-12-2007, 07:09 AM
  5. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21