[Programming question] delete a specific character from a string

Source: Internet
Author: User
Tags first string

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;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.