?
Solved

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

Posted on 2003-03-02
12
Medium Priority
?
458 Views
Last Modified: 2010-04-15
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
Comment
Question by:muff_2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
12 Comments
 

Expert Comment

by:BlueTrin
ID: 8054469
'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
 

Author Comment

by:muff_2000
ID: 8054506
What do i need to use  to read more characters?
0
 
LVL 7

Expert Comment

by:MrNed
ID: 8054652
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!

 

Expert Comment

by:gotenks
ID: 8054668
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
 

Author Comment

by:muff_2000
ID: 8054678
I have already change the return 0; but it does the same thing.
Any other Suggestion?
0
 
LVL 7

Expert Comment

by:MrNed
ID: 8054683
ok, i was saying that it returns zero on the first iteration so it will never loop.
0
 

Author Comment

by:muff_2000
ID: 8054688
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
 

Expert Comment

by:gotenks
ID: 8054829
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
 

Expert Comment

by:gotenks
ID: 8054850
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
 
LVL 1

Accepted Solution

by:
TheBeaver earned 200 total points
ID: 8055325
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
 
LVL 1

Expert Comment

by:TheBeaver
ID: 8055331
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
 

Author Comment

by:muff_2000
ID: 8055558
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question