设计一程序实现功能,处理字符串A,处理规则是只要B字里面有的字母,不分大小写,一律从A 字符串中删掉。
char *delChar(char* A, const char* B){
int c; //记录A中相同的字符数且与B中字符相同的情况
while(*B){
if((*B < 'A' || *B > 'Z') && (*B < 'a' || *B > 'z')){
B++;
continue;
}
for (int i = 0; A[i]; ++i) {
//相同字符就删除A中的字符
/*if((*B == A[i] || (*B)-32 == A[i] || (*B)+32 == A[i]) &&
(A[i] == A[i+1]|| A[i]-32 == A[i+1] || A[i]+32 == A[i+1]))
c++;
else if(*B == A[i] || (*B)-32 == A[i] || (*B)+32 == A[i]){
for (int j = i; A[j] != '\0'; ++j)
A[j-c] = A[j+1];
i -= (c-1);
c = 0;
}*/
//调用tolower函数把大小写字母转变为小写字母来比较
if(tolower(*B) == tolower(A[i]) && tolower(A[i]) == tolower(A[i+1]))
c++;
else if(tolower(*B) == tolower(A[i])){
for (int j = i; A[j]; ++j)
A[j-c] = A[j+1];
//因为删除了相同的字符,不同字符往前遍历了但还没比较后面的字符,所以要重新在同位置开始比较
i -= (c-1);
//重新计数
c = 0;
}
}
B++;
}
return A;
}
测试
int main(){
char A[] = "Aasd WQqQq ,q!!FVRqWG";
char* B = "qWa";
printf("%s",delChar(A , B));
return 0;
}
终端输出
sd ,!!FVRG
标签:字符,函数,int,32,DeleteChar,char,++,tolower
From: https://www.cnblogs.com/Mr--Song/p/18178321