-2

I am working with a pointer to an array of characters. This code is supposed to switch the cases of letters, delete digits, print two spaces instead of one, and print all other chars the same. It does all the rest fine except print other characters the same. This seems like it should be a non issue but I cannot figure it out. See anything that looks like its wrong?

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");
}

for example, if given the lines:

CONSECUTIVE_LINE
CONSECUTIVE_LINE
CONSECUTIVE_LINE
123 REPEAT 
123 REPEAT 
232unique-line

the output will be:

consecutiveline
  repeat
UNIQUELINE

representing a deletion of consecutive lines, the changing of cases, adding two spaces in the place of one and the deletion of digits. However, it will not print the normal underscores and other characters not targeted.

z.rubi
  • 327
  • 2
  • 15

1 Answers1

1

This test...

*(array1+i) >= '0' || *(array1+i) <= '9'

... will always yield true. Any character you check against is going to be more than '0' or less than '9' because '0' < '9'. You probably wanted to check a character is inside this range, which requires a && (logical AND), like you do in all the others.

As a side note, don't assume the character encoding is going to put the alphabetic characters in sequence. It's only guaranteed to be true for the digit characters. A better check would utilize isalpha and islower or isupper from the standard library's ctype.h header.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458