使用 loop_in_C 一次获取一个字符
Getting a character at a time with loop_in_C
我正在阅读 C 语言书籍,我坚持使用以下代码...正如我展示的 C 代码一样,我正在使用 for() 循环获取 char in.and 与我使用 for 的方式相同循环在屏幕上打印一个字符...如果用户按下回车键,循环将退出,另一个用于在屏幕上打印的 for() 循环将使用 i 变量的值。但是屏幕上的结果是相反的。我能听听你的意见吗?我该如何解决?
#include <string.h>
#include <stdio.h>
int main()
{
int i;
char msg[25];
printf_s("Type up to 25 characters then press Enter..\n");
for (i = 0; i < 25; i++)
{
msg[i] = getchar();// Gets a character at a time
if (msg[i] == '\n'){
i--;
break;// quits if users presses the Enter
}
}putchar('\n');
for (; i >= 0 ; i--)
{
putchar(msg[i]);// Prints a character at a time
}putchar('\n');/*There is something wrong because it revers the input */
getchar();
return 0;
输入后,变量i
保存msg
中的确切字符数。这就是为什么有 i--
语句的原因,这样当您输入 ab<enter>
时,您将得到 i==2 而不是 i==3.
第二个循环向后计数到 0,这不是您想要的。您需要从 0 数到 i
。现在你不能指望我使用我。您需要两个变量:一个用于保持最大值,一个用于实际计数。
我会留给你来决定具体怎么做,因为这是学习的一部分。
使用qsort
排序,写法如下
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *, const void *);
int main(void){
int i, n;
char msg[25];
printf_s("Type up to 25 characters then press Enter..\n");
for (i = 0; i < 25; i++){
int ch = getchar();
if(ch == '\n' || ch == EOF)
break;//if (msg[i] == '\n'){i--; <<- this cannot be 25 character input.
else
msg[i] = ch;
}
putchar('\n');
n = i;
qsort(msg, n, sizeof(char), cmp);//sizeof(char) : 1
for (i = 0; i < n ; ++i){
putchar(msg[i]);
}
putchar('\n');
getchar();
return 0;
}
int cmp(const void *a, const void *b){
unsigned char x = *(const char *)a;
unsigned char y = *(const char *)b;
return (x < y) ? -1 : (x > y);//Ascending order
}
我正在阅读 C 语言书籍,我坚持使用以下代码...正如我展示的 C 代码一样,我正在使用 for() 循环获取 char in.and 与我使用 for 的方式相同循环在屏幕上打印一个字符...如果用户按下回车键,循环将退出,另一个用于在屏幕上打印的 for() 循环将使用 i 变量的值。但是屏幕上的结果是相反的。我能听听你的意见吗?我该如何解决?
#include <string.h>
#include <stdio.h>
int main()
{
int i;
char msg[25];
printf_s("Type up to 25 characters then press Enter..\n");
for (i = 0; i < 25; i++)
{
msg[i] = getchar();// Gets a character at a time
if (msg[i] == '\n'){
i--;
break;// quits if users presses the Enter
}
}putchar('\n');
for (; i >= 0 ; i--)
{
putchar(msg[i]);// Prints a character at a time
}putchar('\n');/*There is something wrong because it revers the input */
getchar();
return 0;
输入后,变量i
保存msg
中的确切字符数。这就是为什么有 i--
语句的原因,这样当您输入 ab<enter>
时,您将得到 i==2 而不是 i==3.
第二个循环向后计数到 0,这不是您想要的。您需要从 0 数到 i
。现在你不能指望我使用我。您需要两个变量:一个用于保持最大值,一个用于实际计数。
我会留给你来决定具体怎么做,因为这是学习的一部分。
使用qsort
排序,写法如下
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *, const void *);
int main(void){
int i, n;
char msg[25];
printf_s("Type up to 25 characters then press Enter..\n");
for (i = 0; i < 25; i++){
int ch = getchar();
if(ch == '\n' || ch == EOF)
break;//if (msg[i] == '\n'){i--; <<- this cannot be 25 character input.
else
msg[i] = ch;
}
putchar('\n');
n = i;
qsort(msg, n, sizeof(char), cmp);//sizeof(char) : 1
for (i = 0; i < n ; ++i){
putchar(msg[i]);
}
putchar('\n');
getchar();
return 0;
}
int cmp(const void *a, const void *b){
unsigned char x = *(const char *)a;
unsigned char y = *(const char *)b;
return (x < y) ? -1 : (x > y);//Ascending order
}