Thread: filtering

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    8

    filtering

    Need help with this problem:
    Convert all the letters in file to lower case, and any non letter character is converted to a space. Consecutive spaces should be compressed into one single space.

    This is what I have so far:

    Code:
    #include <stdio.h>
          #define FALSE 0
          #define TRUE 1
    
    int replace(int c){
      
      if (ispunct(c) || isdigit(c))
      {
        putchar(' ');
      
          while ((c = getchar() ) != EOF && ispunct(c) || isdigit(c))
          { }
      }
      if (c != EOF)
      
        return c; 
    }
    
    
    int main()
    {
         int c;
         int start= TRUE;
    
          while ( (c = getchar() ) != EOF)
          {
            if (isspace(c))
            {
    
               if(start==FALSE)
               putchar(' ');
          
               while ( (c = getchar() ) != EOF && isspace(c))
               {}
            } 
               if (c != EOF)
               {        
               putchar(tolower(replace((c))));
                  start = FALSE;
               }
          } printf("\n");
    }

    The test file consists of:
    Code:
    Hello1431387409876187461203470981743Hello                   (works)
    Hello@$#%@%#$%@#$%^&(*%&^(*$%*Hello                   (works)
    Hello!2@3$4%4^7&6&5$6&5%4%3$2$5$3Hello                    (works)
    Hello11$%33%%3452345$%$%#45453$%Hello                   (works)
    Hello                               Hello                   (works)
    Hello            $1%4#             Hello                   (doesn't work)
    desired output:
    Code:
    Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
    current output:
    Code:
    Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello      Hello
    hope this is clear, any suggestions?
    Last edited by kezkez; 07-03-2010 at 10:59 AM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    When you encounter a whitespace character, if this is the first character between words, output a space. Otherwise, skip to the next word. Work that idea into your replace function or main function.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Another way to look at the problem is to consider your program as a simple state machine. There are really three modes your program can be in: inside a word, handling the first space, and handling subsequent spaces. The middle mode can be viewed as a transition between the other two modes. If you look at the problem this way the solution is really quite simple; it goes something like this.
    Code:
    enum mode_t {
        MODE_IN_WORD,
        MODE_BETWEEN_WORDS
    };
    
    mode_t mode = MODE_IN_WORD;
    
    while((c = getchar()) != EOF) {
        if(mode == MODE_IN_WORD) {
            if(isalpha(c)) {
                putchar(tolower(c));
            }
            else {
                /* transition to the state MODE_BETWEEN_WORDS */
                mode = MODE_BETWEEN_WORDS;
                putchar(' ');
            }
        }
        else {  /* MODE_BETWEEN_WORDS */
            if(isalpha(c)) {
                /* transition to the state MODE_IN_WORD */
                putchar(tolower(c));
                mode = MODE_IN_WORD;
            }
            else {
                /* ignore this character */
            }
        }
    }
    That's not perhaps the most elegant way of doing things, but I hope it's understandable. If there were more than two states it would be nice to have a more structured definition of what a state is and how one transitions between them, but as it is I think that would do the job . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    8
    Looking at it from that perspective was very helpful dwks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Filtering the return key
    By SilentPirate007 in forum C Programming
    Replies: 1
    Last Post: 03-09-2010, 04:41 PM
  2. filtering a database
    By MarlonDean in forum C++ Programming
    Replies: 0
    Last Post: 04-07-2009, 12:19 AM
  3. Internet Filtering
    By zacs7 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 01-04-2009, 06:40 AM
  4. packet filtering and/or firewalls
    By obsidian in forum Networking/Device Communication
    Replies: 1
    Last Post: 11-09-2003, 03:16 AM
  5. help needed - filtering a data file (with strings?)
    By Galligan in forum C Programming
    Replies: 1
    Last Post: 11-18-2002, 10:20 AM