Thread: How do I remove a character from character array?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    How do I remove a character from character array?

    I am tryin gto make a program that allows the user to enter a string as a character array and a letter to delete from the array. I am having a very hard time deleting the letter from the array. The word can not have a space where the letter used to be. So if the user enters "hello world" and the letter 'l'. It should print "heo word". Can someone please point me in the right direction here? Thanks

    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    	void remove(char[], char);
    	char letter ;
    	const int MAX = 55;
    	char cstring[MAX];
    
    	cout << "Enter sentence and a letter will be removed\n";
    
    	cin.getline(cstring, MAX);
    
    	cout << "Enter letter to be removed";
    	letter = cin.get();
    
    	remove(cstring, letter);
    
    	cout << cstring << endl;
    
    	cin.ignore();
    	
    
    	return 0;
    }
    
    void remove(char cstring[], char letter)
    {
    	char temp;
    	
    	
    	for(int i = 0; cstring[i] != '\0'; i++)
    	{
    
    		if(cstring[i] == letter) cstring[i] = 		
    			
    	}
    
    	
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't just "remove", you also have to "paper over the hole" by moving other letters over to the left to take their place. (And yes, you'll have to move the whole rest of the string down.)

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    I got it to work like this
    Code:
    void remove(char cstring[], char letter)
    {
    	char cstring2[55];
    	
    	for(int i = 0; cstring[i] != '\0'; i++)
    	{
    
    	if(cstring[i] != letter)  cout << cstring[i];	
    			
    	}
    
    	
    }
    But I need to print the string in main, not in the remove() function. So I was thinking of using strcpy, to try and copy all letters except the specified letter into a new character array. Is this possible? I looked up strcpy and found no one is using it like I'm trying to, but maybe there is good reason for that?
    Last edited by nick753; 12-07-2010 at 08:19 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As I recall, you can't use strcpy on "overlapping" things (i.e, if the source and destination are in the same place in memory). memmove might work in that respect. I would still suggest shifting the whole thing over yourself, though.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Can you give me some idea how to do that?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you have to delete character 4:
    Put character 5 in the 4th spot.
    Put character 6 in the 5th spot.
    Put character 7 in the 6th spot.
    Put character 8 in the 7th spot.
    .
    .
    .
    Put character (last) in the (last-1)th spot.

    Presumably you'll do that in a loop. Since it's a string, once you reach the \0 character you are done.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by tabstop View Post
    If you have to delete character 4:
    Put character 5 in the 4th spot.
    Put character 6 in the 5th spot.
    Put character 7 in the 6th spot.
    Put character 8 in the 7th spot.
    .
    .
    .
    Put character (last) in the (last-1)th spot.

    Presumably you'll do that in a loop. Since it's a string, once you reach the \0 character you are done.
    This will work with the way my program is set up now? Also, this will work if I have to delete character 4, 5, and 11?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nick753 View Post
    This will work with the way my program is set up now? Also, this will work if I have to delete character 4, 5, and 11?
    As long as you're using C-style strings (arrays of char with \0 at the end), then it will work wonderfully. If you have to delete multiple things, you do it multiple times.

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    should I set last to strlen? Like this

    last = strlen(cstring)

    ??

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Why don't you implement the whole algorithm and if it breaks then we'll fix it. Whether something like that is correct depends on what happens.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That will certainly give you the right number.

  12. #12
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by whiteflags View Post
    Why don't you implement the whole algorithm and if it breaks then we'll fix it. Whether something like that is correct depends on what happens.
    I have been trying to create an algorithm for about 3 hours now and nothing I do works. I don't know what to do.

  13. #13
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Isn't there a way that I can store all of the letters that are != to letter into another char array? That is what I was hoping to do with strcpy.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I am pointing out that you ought to post some code instead of asking questions about a random statement you think could be right or wrong. If you seriously have no idea what to do, then you haven't been paying attention, either. Try writing what you think you should do, based on what you've been told, because to be honest, it's very hard to show you more of the algorithm without giving you an implementation.

    Isn't there a way that I can store all of the letters that are != to letter into another char array? That is what I was hoping to do with strcpy.
    If you find all the instances of letter, then that should break up the cstring into sections that you can copy to another destination (as in, not cstring) with strncpy.

  15. #15
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Here is my attempt at doing that, but it is giving me spaces where the letter used to be when I cout the 2nd array.
    Code:
    	for(int i = 0; cstring[i] != '\0'; i++)
    	{
                if(cstring[i] != letter)
                {
                 cstring2[i] = cstring[i];            
                
                 }
     
                   cout << cstring2[i]; 
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character array initialization problem
    By ThatDudeMan in forum C Programming
    Replies: 7
    Last Post: 12-03-2010, 03:56 PM
  2. Replies: 15
    Last Post: 09-23-2010, 02:19 PM
  3. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  4. Converting character array to integer array
    By quiet_forever in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2007, 05:48 AM
  5. Character Array - almost? works
    By voltson4 in forum C Programming
    Replies: 3
    Last Post: 03-04-2003, 06:03 PM