Question:
C++ need help deleting character from char array.?
Nick M
2010-12-07 17:23:17 UTC
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


#include
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] =

}


}
Seven answers:
oops
2010-12-07 17:53:42 UTC
// be sure to include and

void remove_and_erase(char * cstring, char c)

{

    char * end = cstring + strlen(cstring);

    end = std::remove(cstring, end, c);

    *end = '\0';

}



This can be done on a single line, but I think the above is more readable.



void remove_and_erase(char * cstring, char c)

{

    *std::remove(cstring, cstring + strlen(cstring), c) = '\0';

}
2015-08-06 11:00:07 UTC
This Site Might Help You.



RE:

C++ need help deleting character from char 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"...
tfloto
2010-12-07 17:42:34 UTC
This is a trick problem. In order to keep it simple, I would create a temporary string to hold the result and copy back to the original when done.

Also it's more efficient to pass the string as a pointer.

so:

void remove(char *pString, char letter)

{

// ok we're mixing C and C++ here - it's legal a little ugly but...

char * temp = new char[strlen(pString)];

char * sourceStr = pString ;

char * destStr=temp ;



while (*sourceStr != 0)

{

if((*sourceStr != letter)

{

*destStr = *sourceStr ;

destStr++;

*destStr = 0; // always keep it terminated

}

sourceStr++;

}

// done

strcpy(pString, destStr);

delete destStr;

}
Robc
2010-12-07 20:31:51 UTC
Comment on one answer that said that it was more efficient to pass the array as a pointer,



An array is automatically passed by pointer. You do not need to do anything special if you want to use the array as a pointer.



The array name is a pointer to the first element in the array.



void remove(char cstring[], char letter)

{

....char temp;

....int x = 0;

....for(int i = 0; cstring[i] != '\0'; i++)

....{

........if(cstring[i] != letter)

........{

............*(cstring+x) = *(cstring + i); // or *(cstring+x) = cstring[i]; or cstring[x] = cstring[i]; or etc

............++x;

........}

....}

....*(cstring+x) = 0; // or cstring[x] = '\0'

}
?
2010-12-07 18:15:07 UTC
One method you might try is dividing the string into tokens, using the C function strtok(). The delimiter is a string representation of the character you want to delete. Printing the individual tokens to the screen effectively deletes the delimiter character:



char str[] = "Hello world";

char c[] = "l";

char* p = strtok(str, c);

while(p != NULL)

{

printf("%s", p);

p = strtok(NULL, c);

}





Another way would be to use the C++ library, iterate over the characters in the string, and use the function erase() to remove the desired character. Using the std namespace:



string str = "Hello world";

char c = 'l';

string::iterator it;

for(it = str.begin(); it < str.end(); it++)

{

if(*it == c)

{

str.erase(it);

it--;

}

}
2016-03-13 14:21:07 UTC
Let us think line is the given char array char* extractAlphabets(char* line){ char* alphaline; for (int i = 0, j = 0; *(line + i) != "\0"; i ++){ int asciiCode = (int)*(line + i); if ((asciiCode > 64 && asciiCode < 91) || (asciiCode > 96 && asciiCode < 123)){ *(alphaline + j) = *(line + i); j ++ } } return alphaline; } -Ravindra Gullapalli
mkzx
2010-12-07 17:37:42 UTC
How about something like:



bool match = false;

for(int i = 0; cstring[i] != '\0'; i++)

{

if(cstring[i] == letter)

match = true;



if( match )

cstring[i] = cstring[i+1];



}





This wont deal with multiple letter removal, (just one) but it should give you an idea how to go about it... (so rather than bool match, maybe int numMatches)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...