字符串输入丢弃多余的\n 或者\0的方法。
mingw吧
全部回复
仅看楼主
level 5
1. 自己编写的s_get() 函数
2. 用<string.h>strchr()函数
(证明下自己还在学习。)
2019年03月27日 06点03分 1
level 5
/* 该程序读取输入行,删除储存在字符串中的换行符,
如果没有换行符,则丢弃数组装不下的字符*/
# include <stdio.h>
#define STLEN 10
int main(void)
{
char words[STLEN];
int i;
puts("Enter strings (empty line to quit):");
while ( fgets(words,STLEN,stdin) != NULL
&& words[0] != '\n')
{
i = 0;
while (words[i] != '\n' && words[i] != '\0')
i++;
if(words[i] =='\n')
words[i] = '\0';
else
while (getchar() != '\n')
;
puts(words);
}
puts("done");
return 0;
}
2019年03月27日 06点03分 2
level 5
# include <stdio.h>
# include <string.h>
#define STLEN 10
int main (void)
{
char line [STLEN];
char * find;
fgets(line, STLEN, stdin);
find = strchr (line, '\n');
if (find != NULL)
*find = '\0';
else
while (getchar() != '\n')
continue;
puts(line);
fflush(stdin);
getchar();
return 0;
}
2019年03月27日 06点03分 3
level 5
2019年03月27日 06点03分 4
level 13
发在C语言吧会有更多人看到。[呵呵]
2019年03月27日 23点03分 5
1