• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 462
  • Last Modified:

Program that Counts the letter combination (on) in a sentence.

I have this program that has to count the letter combinnation (on) in a sentence via file input redirection.
And when he encounters the letter combination (end) it will stop .
For instance end =EOF.
It compiles with zero error but doesn't gave an output.
Any help will be appreciated


#include <stdio.h>
     
 
     int on(char);                    /*function prototypes*/
     int end(char);

main()
{
    char c;
     
    while (1) {
          scanf("%c",&c);
     
          if (end(c) <= 0)
               break;
     }
                                   /* Display the number of words found*/
          printf("There were %d occurrences of the letter combination \"on\"\n",on(c));                        
   
    return 0;

}


     
                                   /*Function that looks for the ON word*/

int on(char y)
{
     
          int letter_count= 0;
          int found_o=0;

     if ((y == 'o') || (y == 'O')){
          found_o=1;
     }
     if (found_o &&((y =='n') || (y =='N'))){ ++letter_count;    
          return letter_count;
     }
     else found_o=0;    
     
}



                                   /*Function that looks for end word for EOF*/



int end(char c)
    {  static int found_en = 0;
       static int found_e = 0;
       
       if (found_en)
       {  found_en = 0;
          if ('d' == c) return 1;
          else if ('e' == c) found_e = 1;
       }
       else if (found_e)
       {  found_e = 0;
          if ('n' == c) found_en = 1;
          else if ('e' == c) found_e = 1;
       }
       else found_e = ('e' == c);
       return 0;
    }

0
muff_2000
Asked:
muff_2000
  • 4
  • 3
  • 2
  • +2
1 Solution
 
BlueTrinCommented:
'char c' is only 1 character like 'a' 'h' or 'e'.

if you really cannot figure out how to solve it, i ll post a listing ... (need to rest some hours before to get back to work =p)
0
 
muff_2000Author Commented:
What do i need to use  to read more characters?
0
 
MrNedCommented:
hehe,

take a closer look at the last line of your end() function :)

int end(char c)
 blah blah blah
 return 0; // AAARRRRRRRRRRRGGGGGHHHHHHHH!!!!!!!!!!
}
 
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gotenksCommented:
you need to use a char array to read in more than one character.

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

int main() {
   char buf[50];
   int word_on = 0;

   printf("Enter text : ");
   while ( scanf("%s", buf) && strcmp(buf, "EOF") != 0 ) {
      if ( strcmp(buf, "ON") == 0 || strcmp(buf, "on") == 0 /* and other 'on' letter combination */ ) {
         word_on++;
      }
//      printf("Text entered : %s\n", buf);
   }

   printf("Total word 'ON' found : %d\n", word_on);
   return 0;
}

this example will read in a line of text from the console, and the count the total occurences of the 'on' word.
if you want to read from file, i think you just need to change the scanf() to fscanf(). hope this help.
-- sample input --
Enter text : spoon is on the table. EOF
-- end of sample input --
0
 
muff_2000Author Commented:
I have already change the return 0; but it does the same thing.
Any other Suggestion?
0
 
MrNedCommented:
ok, i was saying that it returns zero on the first iteration so it will never loop.
0
 
muff_2000Author Commented:
I can not use array with this program.
I am restircted to a while statement with three functions including the main function.

thank you
for you help and time
0
 
gotenksCommented:
can you use global variables? i have test it out and found that your on() is not right (bug) for the last 'else statement'.

try to debug this : let say the char y now is 'o', from the first if statement, it is true. then the found_o will be set to 1. after that it will test for the second if statement, where it tries to check if y is 'n' or not (if you remember carefully, it is still 'o', as this is still the first loop. therefore, the test will fail and will go into the else statement which will then reset the found_o to 0. therefore you wont be able to test if the next character is a 'n' or not.
i have tried to modify the code and here it is, see if this help...

#include <stdio.h>
   /*function prototypes*/
   void on(char);
   void end(char);

   /* global variables */
   int found_end = 0;
   int found_on = 0;

int main() {
   char c;
   
   while (! found_end) {
      scanf("%c",&c);
/*         printf("%c", c); */
      on(c);
      end(c);
/*         if (end(c) <= 0)
              break; */
   }

   /* Display the number of words found*/
   printf("There were %d occurrences of the letter combination \"on\"\n",found_on);                        
   
   return 0;
}
 
/*Function that looks for the ON word*/
void on(char y) {
/*         int letter_count= 0; */
   static int found_o;

   if ((y == 'o') || (y == 'O')){
/*      printf("o\n"); */
      found_o=1;
   }

   if ((found_o == 1) &&((y =='n') || (y =='N'))){
      found_on++;
/*      printf("n\n"); */
/*      return letter_count; */
   }
   else if ( (y=='o') || (y=='O')) {
      /* do nothing */
   }
   else {
      found_o = 0;
   }
}

/*Function that looks for end word for EOF*/
void end(char c) {  
   static int found_en = 0;
   static int found_e = 0;
   
   if (found_en) {  
      found_en = 0;
      if ('d' == c) found_end = 1;
      else if ('e' == c) found_e = 1;
   }
   else if (found_e) {
      found_e = 0;
      if ('n' == c) found_en = 1;
      else if ('e' == c) found_e = 1;
   }
   else found_e = ('e' == c);
//      return 0;
}
0
 
gotenksCommented:
however, the above code is not the correct solution, as when you try to enter the following : 'ononn', it will return 3 occurences, so you need to do extra checking for that... maybe you shouuld try it yourself.
my guess would be, reset the found_o to 0 after you have found the 'on' and check for 'blank spaces', maybe... :) good luck.
0
 
TheBeaverCommented:
int on(char current)
{
  static char last=0;
  int rv=0;

  if( (last=='o') || (last=='O') )
    if( (current=='n') || (current=='N'))
      rv = 1;

  last = current;

  return rv;
}

int eof(char current)
{
  static char last=0, last2=0;
  int rv=0;

  if( (last=='e') || (last=='E') )
    if( (last2=='o') || (current=='O'))
      if( (current=='f') || (current=='F'))
        rv = 1;

  last = last2;
  last2= current;

  return rv;
}

main()
{
  char ch;
  int  count=0;

  while( !eof(ch) )
  {
    ch = getch();
    count+=on(ch);
  }
  // printf here
}
0
 
TheBeaverCommented:
On course if you meant that the EOF was the ASCII character for EOF (0) then...

int on(char current)
{
 static char last=0;
 int rv=0;

 if( (last=='o') || (last=='O') )
   if( (current=='n') || (current=='N'))
     rv = 1;

 last = current;

 return rv;
}

main()
{
 char ch=1;
 int  count=0;

 while( ch )
 {
   ch = getch();
   count+=on(ch);
 }
 // printf here
}
0
 
muff_2000Author Commented:
Thank you it really helped.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now