Character recognition...

This is a discussion on Character recognition... within the C Programming forums, part of the General Programming Boards category; Hi, I'm a lil new to this C stuff, but I am having some trouble finding a way to count ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    11

    Character recognition...

    Hi, I'm a lil new to this C stuff, but I am having some trouble finding a way to count the number of times the "on" characters appear in a paragraph. The paragraph comes from a seperate input file and has to be sent out to an output file with a summary of information about the paragraph, one thing being the number of times the "on" characters appeared. Any help is greatly appreciated.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post what you've tried so far and someone will help you.

    Basically:

    - read a character
    - if it's one you're interested in, increment counter
    - repeat until end
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    okay,

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
         in c;
         while ((c = getchar()) != EOF)
               if (c='o'))
    This is where I get stuck, b/c it has to ignore whitespace and new lines. Yet it must check for the 'n' also. Thanks again, as always any help is greatly appreciated!

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    "on" is not a character, it is a string, might want to try strstr to look for it, and when comparing characters for equality like you did in your previous post (or other variables for that matter), you use ==, not = which is the assignment operator.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    i'm not familiar with strstr..
    and == comment is noted.. thank you

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    if anyone could help me with strstr or another method... I would appreciate it

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    mabey you should use strtok, I haven't used it much, it could be done with strstr like I stated earlier, but trying to write the code came out clunky and inefficient.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    okay, i have another quick question.. I'm making a word count program too and it isn't giving me the output for some reason.. i've been up for like 35 hours so it is astonishingly hard to read code right now.. but again thanks in advance

    Code:
    // the following code counts the number of words
    
    #include <stdio.h>
    #include <ctype.h>
    
    int found_next_word(void);
    
    int main (void)
    
    {
     int word_count=0;
    
     while(found_next_word()==1)
        ++word_count;
     printf("Number of words = %d\n\n", word_count);
    
    return 0;
    }
    
    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;
    }

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Here are some examples that do *almost* what you're trying to do.

    Using strstr:

    Code:
     int count(const char * buff, const char * what)
    {
     int cnt = 0;
     
     while((buff=strstr(buff,what))!=0) {
         ++cnt, buff++;
        }
     return cnt;    
    }
    And using a more 'manual' approach.

    Code:
     int count(const char * buff, const char * what)
    {
     int cnt = 0;
     int good = 0;
     int i = 0;
     int j = 0;
     
     while(buff[i]!=0) {
     
          if(good==0) { //reset
               j=0;
              }
          else if(what[j]==0) { //got it
               ++cnt;
               j=0;
              }    
    
          if(buff[i]==what[j]) { //so far, so good
               good=1;
              }
          else { 
               good=0;
              } 
                  
          ++i, j++;    
         }
         
     if(what[j]==0) {
          ++cnt;
         }    
         
     return cnt; 
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    Okay, here's what I am ultimatly trying to do:

    Given an input paragraph find the number of words, lines, and number of times the character combination "on" appears. Can anyone here help me combine some of this?

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Slightly confused. Based on what you said here:
    Originally posted by miltecnico
    ...it has to ignore whitespace and new lines. Yet it must check for the 'n' also.
    Are we to assume in this paragraph:
    Only on the way to nowhere can the two
    n
    ew members reach one answer on Tuesday
    there are 6, 4, or 2 "on"s?

    For this part of your problem and using the code you posted first, if you find an "o" simply read the next character and process it accordingly within the if
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    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

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    regarding new lines.. Given an input paragraph it has to read in each character and the program will eventually cipher the paragraph using a different alphabet and when a "'/n'"character is read in it will be replaced with the same "'/n'" character

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always do it the easy way...
    Code:
    while( (c=fgetc( fp )) != EOF )
    {
        if( c == 'o' )
        {
            if( (c = fgetc( fp )) == 'n' )
                count++;
            else
                ungetc( c, fp );
        }
    }

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

  15. #15
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    would you be willing to explain that a little, I'm kind of new to this still?

Page 1 of 2 12 LastLast
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, 01: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, 04:32 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

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