看了些网上的答案,对于不同长度的字符串,去掉相同字符后,得到的字符串不是很全。
#include
#define MAXLEN 100 //字符串长度为100,可以自己定义
int my_getline(char ch[] , int len); //把输入的字符,保存在字符数组中。
void my_squeeze(char ch1[] , char ch2[]); //把两个字符串进行对比,去掉相同字符
int main(void){
char line1[MAXLEN];
char line2[MAXLEN];
int len1 = 0;
int len2 = 0;
while( (len1=my_getline(line1,MAXLEN)) > 0 && (len2=my_getline(line2,MAXLEN)) > 0 ){
printf("over line1:%s , over line2:%s\n",line1,line2);
my_squeeze(line1 , line2);
printf("now line1:%s , now line2:%s\n",line1,line2);
}
return 0;
}
int my_getline(char line[] , int l){
int i = 0;
int c = 0;
for(i = 0;i < MAXLEN-2 && (c = getchar()) != EOF && c != '\n';++i){ //-2是为了保存'\n'和'\0'
line[i] = c;
}
if(c == '\n'){
line[i] = c;
++i;
}
line[i] = '\0';
return i;
}
void my_squeeze(char ch1[] , char ch2[]){
int i = 0 , j = 0 , t = 0;
int m = 0, n = 0;
while(ch1[m] != '\0')
m++;
while(ch2[n] != '\0')
n++;
for(t = 0 ; ch1[t] != '\0' || ch2[t] != '\0'; t++){
if(ch1[t] != ch2[t] && ch1[t] != '\0' && ch2[t] != '\0'){
ch1[i++] = ch1[t];
ch2[j++] = ch2[t];
}
else if(ch2[t] == '\0' && t < m-1){ //如果ch2比较短的话,ch1的字符直接顺序存储
ch1[i++] = ch1[t];
}
else if(ch1[t] == '\0' && t < n-1){
ch2[j++] = ch2[t];
}
}
ch1[i] = '\0';
ch2[j] = '\0';
}