136k views
1 vote
(Find error)

char str[] = "Stop";
if (isupper(str) == "STOP")
exit(0);
Options:
A. There is no error in this code.
B. The isupper function should not be used with a string.
C. The condition should be if (strcmp(str, "STOP") == 0).
D. The exit(0); statement should be removed.

User Koders
by
8.2k points

1 Answer

3 votes

Final answer:

option b,The error in the given code lies with the condition written using the isupper function, which should not be used with a string. The strcmp function should be used to compare strings.

Step-by-step explanation:

The error in the given code is option C. The condition should be written as if (strcmp(str, "STOP") == 0). The problem with the original code is that the isupper function should not be used with a string. The isupper function is used to check if a character is an uppercase letter or not. It takes a single character as an argument, not a string. To compare strings, the strcmp function should be used, which compares two strings and returns 0 if they are equal.

The error in the given code is related to the misuse of the isupper function. This function is designed to check if a single character is uppercase, not an entire string. Therefore, option B is the correct answer. The code snippet incorrectly attempts to compare the return value of isupper (which will not be a string) with the string literal "STOP". A more appropriate way to check if the string is in uppercase would be to compare it with an uppercase version of the original string using the strcmp function after converting str to uppercase, or iterating through each character of the string and using isupper on each character individually.

User AnotherHowie
by
8.8k points