Delete a specific character from a string

Source: Internet
Author: User

#include<stdio.h>#include<assert.h>#include<malloc.h>#include<memory.h>void Delete_specific_char(char * str1, char * str2){      char * phash = (char *)malloc(sizeof(char)*256);   assert(phash);   memset(phash, 0, 256);   int i = 0;   while(str2[i] != '\0' )   {   phash[str2[i++]] =1;   }      int newp = 0;   i = 0;   while(str1[i] != '\0' )   {   if(phash[str1[i]] ==1)   {   i++;   }   str1[newp++] = str1[i];   i++;   }  str1[newp  ] ='\0';     free(phash);}int main(){    char a1[] = "they are student.";   char a2[] = "aeiou"; Delete_specific_char(a1, a2); printf("%s",a1); printf("%s\n",a2); return 0 ;}
# Include <stdio. h> # include <malloc. h> # include <assert. h> # include <memory. h> void Delete_specific_char (char * str1, char * str2) {assert (str1); assert (str2); char * phash = (char *) malloc (sizeof (char) * 256); // map str2 to memset (phash, 0,256); int I = 0; while (str2 [I]! = '\ 0') {phash [str2 [I] = 1; I ++;} char * fast = str1; // This pointer is used to scan str1char * slow = str1; // This pointer is used to modify str1, which stores the next position int j = 0; while (fast [j]! = '\ 0') // start scanning str1 {if (phash [* fast]! = 1) // if the letter to which fast points does not exist in str2, fast and slow all point to the next {* slow = * fast; fast ++; slow ++ ;} else // if the letter that fast points to exists in str2, fast points to the next {fast ++; while (phash [* fast]) // Continue scanning, skip all consecutive characters in str2 until a non-str2 character {fast ++;} * slow = * fast; is found; // assign the non-str2 character to the slow ++ character currently pointed to by slow; // Continue scanning, fast ++ ;}} * slow = '\ 0 '; free (phash);} int main () {char a1 [] = "they are student. "; char a2 [] =" aeiouac "; Delete_specific_char (a1, a2); int I = 0; While (a1 [I]! = '\ 0') {printf ("% c", a1 [I]); I ++;} printf (": \ n"); return 0 ;}

 

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.