Thread: Character recognition...

  1. #16
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    respect! A very elegant piece of code quazah
    "Assumptions are the mother of all **** ups!"

  2. #17
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    Okay, this may be jumbled.. but can someone help me out with this and clean it up a little:

    Code:
    
    #include <stdio.h>
    #include <ctype.h>
    
    int found_next_word(void);
    int look_for_on(void);
    
    int main (void)
    
    {
    
    int on_counter=0;
    
    while(look_for_on()==1)
        ++on_counter;
    printf("Number of times the "on" characters appear: %d\n\n", on_counter);
    
    
    
    
    
    
    int word_count=0;
    
    while(found_next_word()==1)
       ++word_count;
    printf("Number of words = %d\n\n", word_count);
    
    return 0;
    }
    
    
    
    // the following code counts the number of words
    
    int found_next_word(void)
    {
    
    int c;
    
    while (isspace(c = getchar()))
     ;                               /*skip white space*/
    if (c != EOF) {                    /*found a word*/
       while ((c = getchar()) != EOF && !isspace(c))
         ;                           /*skip all but EOF and white space*/
       return 1;
    }
    return 0;
    }
    
    
    
    // the following code finds characters "o" followed by "n"
    
    int look_for_on(void)
    {
    
    int on_counter;
    
    	while( (c=fgetc( fp )) != EOF )
    {
        if( c == 'o' )
        {
            if( (c = fgetc( fp )) == 'n' )
                on_counter++;
            else
                ungetc( c, fp );
        }
    }
    }
    Last edited by miltecnico; 03-19-2004 at 12:14 PM.

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, you don't have a file open any place. For it to work in your current code, you'd have to use a global variable for it. I personally stay away from globals as much as possible, but if you're just learning, it may be easier for you at this point in time.

    Next, you're going to need to actually return the value of on_counter in the function look_for_on, otherwise that value is lost at the end of the function, so your counting goes to waste.

    I'd change your loop where you're looking for on to something like:
    Code:
    on_count = look_for_on(  ); /* or pass the file as an argument to the function */
    printf("\'on\' appears %d times.\n", on_count );
    That'll work for starters. Oh, and for an explanation of how the on count works, it loops through the file, pulling one letter at a time. It then checks it to see if it's o. If it is, it pulls one more. If that letter is n, then it increments count. Otherwise, it puts that character back on the file stream so it can check again to see if that one was o the next time through the loop.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    how do i go about returning the value so i don't lose it, in terms of what the coding is... and how can I find the number of lines when I am only given a paragraph of words?

  5. #20
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by miltecnico
    Yes, there are 6 on's in that paragraph. I know the theory behind it already but I don't know the code...


    Given "sno now"
    I need something that would read in 's', 'n', 'o'...
    trigger a loop to then check if it's followed by an 'n'
    and up a counter if yes
    or skip if no,
    and continue to read each letter... 'o','w' until EOF
    Let me rephrase what you are trying to do, because based on what you said, everyone seems confused. I think I have it, but I'm not sure.

    This paragraph
    Only on the way to nowhere can the two
    n
    ew members reach one answer on Tuesday
    has 6 on's in it. Therefore you need to do something with each and every 'o' followed by 'n', ignoring ALL whitespace between.

    Then:
    Code:
    while( (c=fgetc( fp )) != EOF )
    {
        if(c == 'o')     // an O was found
        {
            c = fgetc( fp );  
            while (isspace(c))   // if character is whitespace
            {
                c = fgetc( fp ); // read the next character
            }
    
            if( c == 'n' )       // not whitespace, N?
            {
                on_counter++;
            }
        }
    }
    To compare accurately 'o' and 'O', use the toupper(c) in each compare.

    Also, format your code with proper indentation. It makes it easier to read.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM