63. delete a specific character (string) from the string ).
Question: enter two strings to remove all the characters from the first string.
For example, input "they are students." and "aeiou ",
Then, the first string after deletion becomes "Thy R stdnts .".
My idea: first scan the first string to determine whether it is a character of the second string. If yes, it will be skipped. The number of records to be skipped will move forward without being deleted.
/* 63. delete a specific character (string) from the string ). Question: enter two strings to remove all the characters from the first string. For example, if you enter "they are students." and "aeiou", the first string after deletion becomes "Thy R stdnts .". */# Include <stdio. h> # include <string. h> void moveout (char * C1, char * C2) {bool flag = false; // whether to retain int D = 0; // The reserved characters of the record need to be moved several digits int I, j; For (j = 0; C1 [J]! = '\ 0'; j ++) {flag = false; for (I = 0; c2 [I]! = '\ 0'; I ++) {If (c1 [J] = c2 [I]) {flag = true; D ++; break ;}} if (flag = false) {c1 [J-D] = c1 [J] ;}} c1 [J-D] = '\ 0';} int main () {char a [50] = "I want to go home"; char B [6] = "aeiou"; moveout (a, B); Return 0 ;}
I can see a good method on the Internet, which is much simpler and faster than mine. The string hash is used.
Http://www.cnblogs.com/gina/p/3247177.html is very clear
#include <iostream>#include <cstring>char * string_del_characters( char * const src, const char * const dest ){ int destLen = strlen( dest ); int hash_table[256] = { 0 }; char * p = src; int index = 0; for( int i = 0; i < destLen; i++ ) { hash_table[ (int)dest[i] ] = 1; } while( *p != ‘\0‘ ) { if( 0 == hash_table[(int)*p] ) { src[index++] = *p; } p++; } src[index] = ‘\0‘; return src;}int main( int argc, char ** argv ){ char src[] = "They are students."; char dest[] = "aeiou"; char * pResult = string_del_characters( src, dest ); std::cout << pResult << std::endl;}