[ create a new paste ] login | about

Link: http://codepad.org/GFpEIt0w    [ raw code | output | fork ]

baojie - C++, pasted on Jun 19:
//17 Remove chars from a string
//   Use char array instead of hash map
//2011-06-19

#include <iostream>
#include <string>

using namespace std;

char* remove_chars (char str[], char remove[]){
	int shouldRemove[256];
	for (int i = 0 ; i < 256 ; i++){
		shouldRemove[i] = 0;
	}
	for (unsigned int i = 0 ; i < strlen(remove) ; i++){
		shouldRemove[(unsigned int)remove[i]] = 1;
	}
	
	char *to_return = new char[strlen(str)];
	int index = 0;
	for (unsigned  int i = 0 ; i < strlen(str) ;  i++){
		if (shouldRemove[(unsigned int)str[i]] == 0){
			to_return[index++] = str[i];
		}
	}
	to_return[index] = 0;
	return to_return;
}

int main(){
	char str[] = "all man are created equal";
	char remove[] = "ae";
	cout << remove_chars(str, remove);
	return 1;
}


Output:
1
ll mn r crtd qul


Create a new paste based on this one


Comments: