我正在使用指向字符数组的指针。此代码应该切换字母,删除数字,打印两个空格而不是一个,并打印所有其他字符相同。除了打印其他字符外,它还能完成所有其他功能。这似乎应该是一个非问题,但我无法弄清楚。看到任何看起来错误的东西?
void compareDuplicates(FILE * ifp, char mode){
/* size MAXCHARS+1 to hold full array + null terminate*/
char newArray [MAXCHARS +1] = {0};
char oldArray [MAXCHARS +1] = {0};
char * newStr = newArray;
char * oldStr = oldArray;
char * tempStr;
/* fill array, test for EOF*/
while(fgets(newStr,MAXCHARS, ifp)){
//if strings are the same, do not print anything
if(strcmp(newStr, oldStr) !=0){
//else print
testStrings(newStr);
}
//set oldStr pointer to newStr, set newStr pointer to oldStr reset newStr memory
//reset memory of newStr to all null chars
tempStr = oldStr;
oldStr = newStr;
newStr = tempStr;
memset(newStr,'\0',MAXCHARS);
}
}
void testStrings(char * array1){
int i = 0;
char c;
while(*(array1+i) != '\0'){
if(*(array1+i) >= 'A' && *(array1+i) <= 'Z'){
c = *(array1+i)+32;
printf("%c",c);
}
else if(*(array1+i) >= 'a' && *(array1+i) <='z'){
c = *(array1+i)-32;
printf("%c",c);
}
else if(*(array1+i) == ' '){
c = *(array1+i);
printf("%c",c);
printf("%c",c);
}
else if(*(array1+i) >= '0' || *(array1+i) <= '9'){
i++;
continue;
}
else{
c = *(array1+i);
printf("%c",c);
}
i++;
}
printf("\n");
}
例如,如果给出行:
CONSECUTIVE_LINE
CONSECUTIVE_LINE
CONSECUTIVE_LINE
123 REPEAT
123 REPEAT
232unique-line
输出将是:
consecutiveline
repeat
UNIQUELINE
表示连续行的删除,案例的更改,在一个位置添加两个空格以及删除数字。但是,它不会打印正常的下划线和其他非目标字符。