Thread: string manipulation

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    10

    string manipulation

    I'm looking for a small piece of code or a function that takes a string as input and removes all characters other than letters and digits from that, and presents the result as output. It's a couple of lines in perl...not sure how to do it in C. If anyone has any ideas let me know. Thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Look in ctype.h for isalnum, isdigit and isalpha:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main ( void )
    {
      int c;
    
      while ( ( c = getchar() ) != '\n' ) {
        if ( isalnum ( c ) )
          putchar ( c );
      }
    
      return 0;
    }
    or
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main ( void )
    {
      int c;
    
      while ( ( c = getchar() ) != '\n' ) {
        if ( isalpha ( c ) || isdigit ( c ) )
          putchar ( c );
      }
    
      return 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:41 AM