标签:io ar for on c ui har as 字符串
定义一个函数,输入两个字符串,从第一个字符串中删除在第二个中出现过的所偶字符串。例如从第一个字符串"We are students."中删除第二个字符中“auiou”中出现过的字符得到的结果是"W r stdnts"
解题思路:利用哈希查找。
#include <stdio.h> void deleteStr2FromStr1(char* str1, char* str2) { if(str1 == NULL || str2 == NULL || *str1=='\0' || *str2 == '\0') return; int hash[256]; for(int i=0; i<256; ++i) hash[i] = 0; char* p = str2; while(*p != '\0') hash[*p++]++; char* strFront = str1; int len = strlen(str1); int small = -1; for(int i=0; i<len; ++i) { if(hash[*(strFront+i)] == 0) str1[++small] = *(strFront + i); } str1[++small] = '\0'; } int main() { char str1[] = "We are students"; char str2[] = "aeiou"; deleteStr2FromStr1(str1, str2); printf("%s",str1); return 0; }
标签:io ar for on c ui har as 字符串
原文地址:http://blog.csdn.net/walkerkalr/article/details/39001155