195k views
3 votes
The following program reads in a line of characters from the keyboard and echoes only the alphabetic characters within the line. For example, if the input were ""Let's meet at 6:00pm."", then the output should be "Letsmeetatpm". However, the program has one or more bugs. Identify and fix the bug(s).

#include
int main (void) {

char echo = '0';
while (echo != '\\') echo < 'z')
&& (echo >'A'
}

1 Answer

4 votes

Final answer:

The bug in the program is in the condition of the if statement. Currently, the program will print any character that is not a lowercase letter. To fix this, the operators in the condition should be changed to &&, so that both conditions must be true for the character to be printed.

Step-by-step explanation:

The bug in the program is in the conditions of the if statement. The logical operators used are incorrect and should be changed. Currently, the program checks if the input character is greater than 'a' or less than 'z' (which includes all characters greater than 'a' and all characters less than 'z') and if the input character is greater than 'A' or less than 'Z' (which includes all characters greater than 'A' and all characters less than 'Z'). This means that any character that is not a lowercase letter will still be printed, because it will fulfill the first condition. To fix this, the operators should be changed to &&, so that both conditions must be true in order for the character to be printed.

Fixed Code:

#include <stdio.h>

int main (void) {
char echo = '0';
while (echo != '
')
scanf ("%c", &echo);
if ((echo >= 'a' && echo <= 'z')
return 0;
}
User Radoslav
by
8.4k points