How do you remove a character from a string please could anyone tell me how to do it?
How do you remove a character from a string please could anyone tell me how to do it?
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.
remove ! character from a streamWell what one are you talking about?How do you remove a character from a string
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.
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
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... */
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.
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; }
You have a PM.