remove array element

This is a discussion on remove array element within the C Programming forums, part of the General Programming Boards category; i want to a C program to delete an element from an array which can use both index method & ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    7

    remove array element

    i want to a C program to delete an element from an array which can use both index method & character method
    For example
    input : "1 222 333 4444"
    output:"1 22 333 4444"
    if either index = "4" or character ="2" is entered
    it should able to read in/accept any input array of varying length, and the position/index or element in array to be deleted

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    To delete from an array you have to shift every element past the one you delete to the left by 1. If it is the last element in the array, you just set it to 0.

    Post some code and we'll help with any problems.

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    By the way,how do you plan on identifying if the argument entered is an index or an element value? In your example the values are both integers so there is no distinction (other then possibly the bounds of the array).

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Quote Originally Posted by nonpuz View Post
    By the way,how do you plan on identifying if the argument entered is an index or an element value? In your example the values are both integers so there is no distinction (other then possibly the bounds of the array).
    make a menu for it?for the user to choose

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,097
    Quote Originally Posted by Ash Mo Naiming View Post
    i want to a C program to delete an element from an array which can use both index method & character method
    For example
    input : "1 222 333 4444"
    output:"1 22 333 4444"
    if either index = "4" or character ="2" is entered
    This is what "memmove" and strchr, and strrchr are for. strchr and strrchr return a pointer to where a certain character is. So define a delete_elem function to take a pointer argument like this

    Code:
    void delete_elem(char *arr, size_t n, char *s) { 
        memmove(s, s+1, n-(s-arr));
    }
    The s points to where you want to delete.

    Code:
    char telephone[] = "1 234 456 6789";
    
    // delete character at index 2
    delete_elem(telephone, sizeof(telephone), &telephone[2]);
    
    // delete first occurence of '4'
    delete_elem(telephone, sizeof(telephone), strchr(telephone, '4'));
    
    // delete last occurence of '6'
    delete_elem(telephone, sizeof(telephone), strrchr(telephone, '6'));

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,097
    UPDATE: There is a typo in the above, it should be

    Code:
    void delete_elem(char *arr, size_t n, char *s) { 
        memmove(s, s+1, n-(s-arr)-1);
    }

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    THANK YOU! c99tutorial , nonpuz
    i have successfully code out the thingy
    thank you v.v.v.v.v.v.v. much!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of ptrs vs constant ptr to an first element of array
    By monkey_c_monkey in forum C Programming
    Replies: 5
    Last Post: 08-30-2012, 11:39 PM
  2. HELP > How to remove element in a Structure Array
    By Inneart in forum C Programming
    Replies: 5
    Last Post: 10-26-2010, 02:40 PM
  3. vector remove element of element
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2010, 03:24 PM
  4. Remove element from STL list
    By Micko in forum C++ Programming
    Replies: 7
    Last Post: 07-05-2007, 11:07 AM
  5. remove element from vector<string>
    By waxydock in forum C++ Programming
    Replies: 9
    Last Post: 07-01-2005, 07:25 AM

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