By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,335 Members | 2,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,335 IT Pros & Developers. It's quick & easy.

Remove a list of characters from a char array/string

P: 1
I am looking for a more efficient version than the following to remove a list of characters from a char array or string.


#include <iostream>
#include <cstring>

using namespace std;

int main(int argc, char* argv[]) {

char str[] = "table+chair%furniture-futon#bed";
char *t = "+*-/%#";
char *pr = str;

int i = 0;
while(*pr != '\0') {
//if(*pr != t) {
if(strchr(t,*pr) == NULL) {
str[i] = *pr;

cout << strlen(str) << ":" << str << endl;

return 0;


Feb 18 '17 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,197
Try strcpy.

strcpy takes a char* argument for where the copy is to go. In your case that would be the address of the last char to be deleted +1.

strcpy will now copy the rest of your original string on top of the sequence you are deleting and the copy stops at the \0 of the original string.

What do you think?
Feb 19 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.