【发布时间】:2018-10-11 04:35:17
【问题描述】:
我正在使用指向字符数组的指针。这段代码应该切换字母的大小写,删除数字,打印两个空格而不是一个,并且打印所有其他字符相同。除了打印其他字符相同之外,其余的一切都很好。这似乎应该不是问题,但我无法弄清楚。看到任何看起来错误的地方吗?
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
表示删除连续行,改变大小写,在一个位置添加两个空格和删除数字。但是,它不会打印正常的下划线和其他未定位的字符。
【问题讨论】:
-
其中一个和其他的不一样
OR是吗?
标签: c arrays if-statement char arr