Thread: remove ! character from a stream

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

    remove ! character from a stream

    How do you remove a character from a string please could anyone tell me how to do it?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Move everything past the character you want to remove to cover it up.

    For example, "abxcde". If you want to delete 'x', move "cde" left by one position.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    remove ! character from a stream
    How do you remove a character from a string
    Well what one are you talking about?

    I assume you're talking about strings (i.e. "char*"). Say you want to remove the "u" from "pound", what do you do? Well you basically "shift" the contents of the string once to the left, starting from after the "u" to the end of the string. So you "shift" the characters after the "u", which are "nd", one position/index to the left. So "pound" becomes "pond". There are a number of ways to do this, the easiest being a simple for loop. Other methods would be "strcpy", "memcpy", "memmove". The reference for "memmove" has a relevant example.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    13
    thing is i need to remove !#"£$%^&*()_+¬ as well would this work by the way my programming is at the lowest level some syntax would be really really helpful

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    13
    Quote Originally Posted by nadroj View Post
    Well what one are you talking about?

    I assume you're talking about strings (i.e. "char*"). Say you want to remove the "u" from "pound", what do you do? Well you basically "shift" the contents of the string once to the left, starting from after the "u" to the end of the string. So you "shift" the characters after the "u", which are "nd", one position/index to the left. So "pound" becomes "pond". There are a number of ways to do this, the easiest being a simple for loop. Other methods would be "strcpy", "memcpy", "memmove". The reference for "memmove" has a relevant example.
    sorry i have a file that is stored in a stream from a text file hope this helps

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You could just get clever with sscanf() and fgets(). Stopping at unwanted characters before doing another sscanf().

    For example:
    Code:
    char buffer[256];
    char wanted[256];
    
    fgets(buffer, sizeof(buffer), stdin);
    
    sscanf(buffer, "%[^xyz]", wanted);  /* wanted will now contain anything up to 'x', 'y' OR 'z'. */
    /* more sscanf()'s... */

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    OK well the same basic principle applies. We can assume the contents of the file is "pound" and you want to remove the "u". You still have to linearly (i.e. one by one) "shift" the contents after the "u", one character to the right. If you have a longer sequence of chars to remove, say "oun", then simply shift 3 characters instead of 1. The actual char(s) in question doesn't matter as long as they're simple 1-byte ASCII characters.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    13
    Quote Originally Posted by zacs7 View Post
    You could just get clever with sscanf() and fgets(). Stopping at unwanted characters before doing another sscanf().

    For example:
    Code:
    char buffer[256];
    char wanted[256];
    
    fgets(buffer, sizeof(buffer), stdin);
    
    sscanf(buffer, "%[^xyz]", wanted);  /* wanted will now contain anything up to 'x', 'y' OR 'z'. */
    /* more sscanf()'s... */
    i have tried this way but it compiles but just hangs

    update
    i have got it working as such but the output file is blank
    Last edited by Boogyman; 02-24-2010 at 07:21 PM. Reason: update

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Even simpler, just use fgets() and take what you want.

    Code:
    zac@neux:code (0) $ gcc want.c -o want
    zac@neux:code (0) $ ./want 
    We don't any stupid characters! That includes your filthy $100 note. "Haha".
    We don't any stupid characters That includes your filthy 100 note. Haha.
    zac@neux:code (0) $ cat want.c 
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       char buffer[256];
       char * ch = buffer;
       const char unwanted[] = "!#\"£$%^&*()_+¬";
    
       fgets(buffer, sizeof(buffer), stdin);
    
       while(*ch)
       {
          /* If *ch is not found in 'unwanted', we must want it */
          /* ... snip ... You have to do something */
          ++ch;
       }
    
       return 0;
    }

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have a PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 05:10 AM
  3. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  4. pipe
    By smart girl in forum C Programming
    Replies: 4
    Last Post: 04-30-2006, 09:17 AM
  5. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM