![]() |
1
27
您要执行以下操作:
也就是说,当您输入字符串并按“enter”键时,换行符将成为
或者,使用strncmp(),它只比较字符串的n个字符 |
![]() |
2
9
fgets()返回字符串“exit\n”——与get()不同,它保留换行符。 |
![]() |
3
6
正如其他人所说,与
比较的简单答案是准确的
在一般情况下,最好将I/O、标记化、解析和操作分离到各自的阶段。 |
![]() |
4
1
同意戴夫的观点。此外,您可能希望改用strncmp()。然后可以设置比较的长度。 http://www.cplusplus.com/reference/clibrary/cstdio/fgets/ http://www.cplusplus.com/reference/clibrary/cstring/strncmp/ |
![]() |
5
0
我建议您将\n从字符串的末尾剥离,如下所示。 char buf[256]; int len; /* get the string, being sure to leave room for a null byte */ if ( fgets(buf,sizeof(buf) - 1) == EOF ) { printf("error\n"); exit(1); } /* absolutely always null-terminate, the easy way */ buf[sizeof(buf) - 1] = '\0'; /* compute the length, and truncate the \n if any */ len = strlen(buf); while ( len > 0 && buf[len - 1] == '\n' ) { buf[len - 1] = '\0'; --len; } 这样,如果必须将输入的字符串与几个常量进行比较,则不必将\n添加到所有常量中。 |
![]() |
CodySig · 检查c中字符串的第一个字母 7 年前 |
![]() |
Roger That · 比较两个表中的字符串matlab 7 年前 |
![]() |
ali · 比较字符串和中间点在PHP中不起作用 7 年前 |
|
Proteen · strcmp崩溃,即使两个字符串都正常[已关闭] 8 年前 |
![]() |
Nono · C-比较字符数组和字符串 8 年前 |
|
Walid Beydoun · 为什么在C样式字符串上使用==有效? 9 年前 |
![]() |
Shivam Bhalla · strcmp中void表达式的使用无效 10 年前 |
![]() |
Dannark · 如何在C中比较两个以上的字符串? 10 年前 |