Delete char from string?

This is a discussion on Delete char from string? within the C Programming forums, part of the General Programming Boards category; Is it possible to set an index of a char array (string) to NULL. I know that you cant do ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    37

    Delete char from string?

    Is it possible to set an index of a char array (string) to NULL.

    I know that you cant do the following:

    Code:
    int MAXBUFFERSIZE] = 5
    char buffer[MAXBUFFERSIZE];
    buffer[4] = 't';
    buffer[4] = NULL;
    Is there a way to set buffer[4] = ''; or null?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you have a string "abcdef" and you want to remvoe 'c' from the string, you need to "cut it out" by copying the first part of the string, skipping the c and then concatenating the rest onto the first part.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    You could make a linked list to store your string in. (Every node stores a char of the string).
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I guess moving a couple of characters from one place to another is simpler than doing a linked list (you'd also need to reimplement all string functions).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    979
    Code:
    char buffer[MAXBUFFERSIZE];
    buffer[4] = 't';
    buffer[4] = NULL;
    Assigning NULL as above is incorrect. NULL is a null pointer constant, and not the null character. It's possible that on some systems, depending on how NULL is defined, that will work and do what's expected, but it's not guaranteed. To represent the null character, use either 0 or '\0'.

    Just changing NULL to 0 won't solve your problem, but I wanted to point out this (somewhat common) misconception.

    Edit: I realize that you knew the above wouldn't be a solution to your problem; but I'm not sure if you understood why.
    Last edited by cas; 04-23-2009 at 01:43 PM. Reason: Clarification.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >Is it possible to set an index of a char array (string) to NULL.
    Well, it is possible. But you would achieve what you wanted. Like you wanted to skip that index value.

    Instead it would just print string up tp the '\0' char. Here is an example

    Code:
    #include <stdio.h>
    
    int main()
    {
        char str[] = { "Its a bit obvious." };
        
        printf("Before placing NULL : %s\n", str);
        
        str[10] = '\0';
    
        printf("After placing NULL : %s\n", str);
        
        getchar();
        return 0;
    }    
    
    /* my output 
    Before placing NULL : Its a bit obvious.
    After placing NULL : Its a bit
    */
    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    If you want to delete a character from a string you can try these handy functions I found in a book:

    Code:
    $ cat testdel.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void strdelcn(char *s, int n, char c);
    void strdelc(char *s, char c);
    
    int main ()
    {
     char teststr[100];
    
     memset(teststr, 0, sizeof(teststr));
    
     strcpy(teststr, "This is my test string.");
     printf("teststr before strdelcn [%s], len[%d]\n", teststr, (int) strlen(teststr));
     strdelcn(teststr, 1, 's');
     printf("teststr after strdelcn [%s], len[%d]\n", teststr, (int) strlen(teststr));
    
     strcpy(teststr, "This is my test string.");
     printf("teststr before strdelc [%s], len[%d]\n", teststr, (int) strlen(teststr));
     strdelc(teststr, 's');
     printf("teststr after strdelc [%s], len[%d]\n", teststr, (int) strlen(teststr));
    
     exit (0);
    
    }
    
    /*****
                                     strdelcn()
          This function deletes the specified number of
          occurrences of a given character given from a string.
       Argument list:    char *s        the string to be searched
                         int n          the number of occurrences to delete
                         char c         the character to be deleted
       Return value:     void
    *****/
    void strdelcn(char *s, int n, char c)
    {
       while (*s) {
          if (*s == c && n) {
             strcpy(s, s + 1);
             n--;
          } else {
             s++;
          }
       }
    }
    
    /*****
                                     strdelc()
          This function deletes all occurrances of a character from a string.
       Argument list:    char *s        the string to be searched
                         char c         the character to be deleted
       Return value:     void
    *****/
    void strdelc(char *s, char c)
    {
       while (*s) {
          if (*s == c) {
             strcpy(s, s + 1);
          } else {
             s++;
          }
       }
    }
    
    $ ./testdel.exe
    teststr before strdelcn [This is my test string.], len[23]
    teststr after strdelcn [Thi is my test string.], len[22]
    teststr before strdelc [This is my test string.], len[23]
    teststr after strdelc [Thi i my tet tring.], len[19]

  8. #8
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    979
    If you want to delete a character from a string you can try these handy functions I found in a book:
    Code:
    ...
    strcpy(s, s + 1);
    ...
    Unfortunately, this is not valid code. You cannot use strcpy() with overlapping objects. It may work on some systems, depending on how strcpy() is implemented, but there are no guarantees.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can however use memmove.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. String Class Operations
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2003, 02:29 PM
  3. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM
  4. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21