#include <stdio.h>
void convertToUpperCase(char *str) {
int i = 0;
while (str[i] != '\0') {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - ('a' - 'A');
}
i++;
}
}
int main() {
char input[1000];
while (1) {
if (fgets(input, sizeof(input), stdin) == NULL) {
break;
}
// 移除换行符(如果存在)
int len = 0;
while (input[len] != '\0') {
len++;
}
if (len > 0 && input[len - 1] == '\n') {
input[len - 1] = '\0';
}
convertToUpperCase(input);
printf("%s\n", input);
}
return 0;
}
1) This code reads input lines from the user, removes any trailing newline characters, converts all lowercase letters in the string to uppercase, and then prints the modified string. It continues this process in a loop until there's no more input.
2) Hints:
- The ASCII value difference between 'a' and 'A' is used to convert lowercase to uppercase.
- The fgets() function includes the newline character in the input buffer, which is why it needs to be removed before processing.