Thread: Deleting a single character from a string

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Deleting a single character from a string

    Can someone tell me how to delete a character from a string? Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Copy all the characters which follow it down one place in the array
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    There are a couple ways...

    The first that comes to mind involves running through the source string character by character and copying each one to a temp string, except the character you are trying to delete, and then assign the original variable to the address of the temp string.

    To omit the character you are trying to delete, you could use an if that checks whether the current character you are about to copy is the index of the character you are trying to delete, and if it is not then continue with copying that character.

    This is by no means the fastest way, but if you go through the steps, it should be fairly easy to convert each step to the appropriate c code to get the job done.

    You might also google around for the c string functions like strncpy(). Use some imagination and you can easily come up with something that uses strncpy() and is faster than the method I outlined above.

    You can also do what Salem suggested... jeez I'm slow when I post.
    Last edited by jEssYcAt; 02-14-2005 at 04:50 PM. Reason: Salem beat me to it!
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    Code:
    #include <stdio.h>
    
    int main(){
        char str[10] = { 'A','B','C','D','E','F','G','H','I', 0 };
        char chr_to_find = 'C';
        int i = 0, j = 0;
    
        printf( "%s\n",str );
    
        while( str[i] ){
               if( str[i++] != chr_to_find )
                   ++j;
               str[j] = str[i];
        }
    
        printf( "%s\n", str );
        return 0;
    }
    Currently Reading:

    Mathematic from the birth of numbers,
    Effective TCP/IP programming,
    Data Compression: The Complete Reference,
    C Interfaces and Implementations: Techniques for Creating Reusable Software,
    An Introduction to Genetic Algorithms for Scientists and Engineers.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    8
    I just thought of this, so I'm not sure if it will work but
    Code:
    char *str, *tmp, *ptr;
    
    strcpy(str,"a string example");
    
    //lets remove the x in 'example' from this string
    //x is located at index 10
    str[10]='\0';    //now the computer thinks that this is the end of the string
    strcpy(tmp,str);//tmp now has "a string e"
    ptr=str[11];      //ptr now is "ample"
    strcat(tmp,ptr); //tmp now has the final string of "a string eample"
    Looks like it might not be efficient but it could be fun to bug a prof this way

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    try this

    Code:
    while(s[i]!='\0')
    {
            if(s[i]=='C')  continue;
    
            s[j++]=s[i++];
    }
    s[j]='\0'
    regards
    harish

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Why do you people use loops? To illustrate what Salem said:
    Code:
    // Given: s is string, i is index which is to be removed:
    memmove(s+i, s+i+1, strlen(s+i));
    Because strlen(s+i) is one more than strlen(s+i+1), copying strlen(s+i) characters starting from s+i+1 includes the '\0'.

    Ideally, you'd already have the length of s somewhere.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ssharish
    try this

    Code:
    while(s[i]!='\0')
    {
            if(s[i]=='C')  continue;
    
            s[j++]=s[i++];
    }
    s[j]='\0'
    You should know that this code will just cause an infinite loop and will never do what you want it to. Perhaps you should "try this" before you suggest some one else do it.

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

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    very sorry about the code which i posted. it seems to be an infinite looop. quzah thax for finding that it is an infinte loop. but i can't still understand why that loop don't terminate. can any one tell me why is it so..

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Because you don't increment i when the current character is 'C'. You just continue without incrementing. So it hits that letter and the next time it loops it's still on the same letter, and the next time it's still on the same letter, etc. etc. until your CPU throws up and dies of the tedium.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    thanks very much itsme86
    the code is working fine for me now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. Replies: 3
    Last Post: 11-03-2002, 02:14 AM