请编写一个程序,该程序的功能是首先将用户通过键盘输入的若干字符(用EOF结束输入)存入一维数组s中,然后找出数组中具有最大ASCII码值的字符,并且输出该字符以及该字符对应的ASCII码。
要求:
程序中有关输入、输出以及查找等操作必须通过指针完成。
/**
* 从标准输入读取字符串到字符串数组中,终止符为EOF
*/
char* read_stdin_to_char_array() {
int malloc_size = 5;
char* buffer = (char*)malloc(malloc_size * sizeof(char));
char ch = getchar();
int offset = 0;
while (ch != EOF) {
if (offset == malloc_size) {
// 输入字符超出buffer size
malloc_size *= 2;
char* new_buffer = (char*)malloc(malloc_size * sizeof(char));
strcpy(new_buffer, buffer);
free(buffer);
buffer = new_buffer;
}
*(buffer + offset++) = ch;
ch = getchar();
}
return buffer;
}
/**
请编写一个程序,该程序的功能是首先将用户通过键盘输入的若干字符(用EOF结束输入)存入一维数组s中,然后找出数组中具有最大ASCII码值的字符,
并且输出该字符以及该字符对应的ASCII码。
要求:程序中有关输入、输出以及查找等操作必须通过指针完成。 **/
char find_biggest_ascii_char() {
char* char_array = read_stdin_to_char_array();
char res = '\0';
if (strlen(char_array) == 0) {
res = '\0';
} else {
for (int i = 0; i < strlen(char_array); i++) {
if (res < *(char_array + i)) {
res = *(char_array + i);
}
}
}
return res;
} mac上测试的话 ctrl+d是退出的 但是直接输入helloworld^D貌似会把^D前的缓冲数据都给清掉 可以试试hellworld\n^D