Thread: Delete all occurrences of a character

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    69

    Delete all occurrences of a character

    I want do delete all occurrences of a char in a given string, but I really don't see my error in the code. So I hoped someone could help me.

    Here is how I did it:

    Code:
      
    
    char *input = "example_string";
    int len = strlen(input);
    
    char *new_line[len]; 
    char bad = 'e'; //the one to delete    
    
        int i;
        int j = 0;
    
    
        for (i =0; i < len;i++) {
            char next = input[i];
          
           if (next != bad) {
            
                new_line[j] = next;  //here I get a warning : incompatible integer to pointer conversion assigning to char* from char. 
    
    
    
                j++;
            }  
        }
    I get segmentation or bus errors..

    And I also don#t understand the warning I get. new_line is an array of chars, and I am trying to assign to it a certain char.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    new_line is an array of char*, which is to say an array of pointer to char. Drop the *, and you'll have an array of char.

    There's one other problem: strings are terminated with a null character (which has the value zero), and you're not copying this character; strlen() does not take it into account. You'll have to copy that, too (and make sure there's space for it).

  3. #3
    Registered User
    Join Date
    May 2014
    Posts
    69
    Quote Originally Posted by cas View Post
    new_line is an array of char*, which is to say an array of pointer to char. Drop the *, and you'll have an array of char.

    There's one other problem: strings are terminated with a null character (which has the value zero), and you're not copying this character; strlen() does not take it into account. You'll have to copy that, too (and make sure there's space for it).
    Thank you for the advice!

    And I was aware of this \0 char at the end, I usually do it this way when copying an array of chars:

    Code:
    char *copy_me = "example";
    int l = strlen(copy_me) +1;
    char *destination = (char*) malloc(sizeof(char)*l);
    
    strncpy(destination, copy_me, l-1);
    because I read somewhere, when the strings have equal length, the strncpy will add the terminal char automatically, so there I alloc for one more char, so it should be done automatically

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by tinchi View Post
    Thank you for the advice!

    And I was aware of this \0 char at the end, I usually do it this way when copying an array of chars:

    Code:
    char *copy_me = "example";
    int l = strlen(copy_me) +1;
    char *destination = (char*) malloc(sizeof(char)*l);
    
    strncpy(destination, copy_me, l-1);
    because I read somewhere, when the strings have equal length, the strncpy will add the terminal char automatically, so there I alloc for one more char, so it should be done automatically
    Strcpy just copies the null byte from arg2 into arg1. Also it might be faster to just use:

    Code:
       if((destination = strdup(copyme)) == NULL){
           perror("strdup"); exit(0); 
       }
    This is with error checking, but strdup malloc()s destination, and strcpy()s copyme into it. It is really good for copying into char* . It's in string.h header.

    Edit: Sorry, just noticed you were using strncpy, I think what you meant was if arg2 is less in length than arg3 a null byte is copied until the length argument is fulfilled.
    Last edited by Alpo; 05-17-2014 at 07:16 PM.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    @Alpo: The `strdup' function is indeed common, but the function is not standard C.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  6. #6
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by phantomotap View Post
    O_o

    @Alpo: The `strdup' function is indeed common, but the function is not standard C.

    Soma
    I see, I usually make a note if a thing is an extension, but missed it here. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-10-2013, 10:58 AM
  2. Remove all occurrences of character from string
    By Scotty33 in forum C Programming
    Replies: 14
    Last Post: 09-27-2011, 04:32 AM
  3. Delete Character
    By Logikal in forum Linux Programming
    Replies: 1
    Last Post: 01-25-2010, 09:23 AM
  4. Delete last Character (comma) ?
    By Hexxx in forum C Programming
    Replies: 2
    Last Post: 10-19-2003, 12:28 AM
  5. char occurrences
    By viaxd in forum C Programming
    Replies: 6
    Last Post: 09-18-2003, 02:19 AM