Home > Software engineering >  What's wrong with this string checking program?
What's wrong with this string checking program?

Time:06-05

I want to make this program only English in the string, but when the end of the string ends in English, another strange word is added. Why is that?

int main(void) 
{

    char line[100];
    char line2[100];    

    printf("Please enter a string: ");
    gets_s(line, sizeof(line));

    int k = 0;
    for(int i = 0; line[i] != '\0';   i) {
        while(line[i] >= 'A'&& line[i] <= 'z') {
            line2 [k  ] = line[i  ];
        }
    }
    line2[k] = '\0';
    printf("string: %s\n", line2);
    return 0;
}

CodePudding user response:

for(int i = 0; line[i] != '\0';   i) {
    while(line[i] >= 'A'&& line[i] <= 'z') {
        line2 [k  ] = line[i  ];
    }
}

replacing the for loop with a while loop...

int i = 0;
while (line[i] != '\0') {
    while (line[i] >= 'A' && line[i] <= 'z') {
        line2 [k  ] = line[i  ];
    }
    i  ;
}

So, here you can see if the inner while goes to the '\0' i gets incremented past the terminating zero byte.

CodePudding user response:

The basic problem is that you put responsibility for incrementing i in two different places. As a result, it can get incremented more than you want -- for example, so as to skip over the string terminator.

It appears to me that you have caused this problem for yourself by making the code more complicated than it needs to be. Why use a two-loop nest to iterate over a single string? In fairness, there are indeed potential reasons to do that, but none of them appear to be in evidence here. I suggest changing the inner while loop to an if statement, and not incrementing i within:

    for (int i = 0; line[i] != '\0';   i) {
        if (line[i] >= 'A' && line[i] <= 'z') {
            line2[k  ] = line[i];
        }
    }

Note how there is only one place where i is incremented in that variation.

Note also that there may be characters that satisfy c >= 'A' && c <= 'z' but are not letters. I say "may" because C is not specific to a particular character set or encoding, but in practice, it is virtually certain that there are non-letter characters in that range on your implementation. Perhaps isalpha((unsigned char) line[i]) would be a better fit, though it is not, by itself, without character set related issues.

  •  Tags:  
  • c
  • Related