关于网友提出的“ 如何从键盘输入得到 gets next_input == NULL ?”问题疑问,本网通过在网上对“ 如何从键盘输入得到 gets next_input == NULL ?”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 如何从键盘输入得到 gets next_input == NULL ?
描述: 输入一组字符串,如果连续几行相同,则将此行输出.
比如输入:
iamlesbian
iamlesbian
iamloli
iamloli
iamloli
iamyourhoney
则输出
iamlesbian
iamloli
弄了个半天好象调试是没错误了,思路估计也正确, 但不知道输入才可以... 就不是知道怎么输入才能得到 gets ( next_input ) != NULL 的结果... 希望大虾们解释一下
#include
#include
#include
#define MAX_COLS 128
int
main ( void )
{
char now_input [MAX_COLS];
char next_input [MAX_COLS];
char output [MAX_COLS];
gets ( now_input ); /* 这个函数的意义更大的再与输入,而不在他返回的指针 */
while ( gets ( next_input ) != NULL );{
/*
** 相同则拷贝到output 中 输出。
*/
if ( !(strcmp ( now_input , next_input )) ){
strcpy ( output , now_input );
puts ( output );
}
strcpy ( now_input , next_input );
}
return EXIT_SUCCESS;
}
解决方案1:#include
#include
#include
#define MAX_COLS 128
int
main ( void )
{
char now_input [MAX_COLS];
char next_input [MAX_COLS];
char output [MAX_COLS];
bool already=false;//加了一个already标志位
gets ( now_input );
while ( gets ( next_input ) != NULL )
{
if ( (strcmp ( now_input , next_input )==0) )
{
if (already==false)
{
strcpy (output , now_input );
puts ( output );
already=true;
}
}
else
{
strcpy (now_input , next_input );
already=false;
}
}
return EXIT_SUCCESS;
}
解决方案2:while ( gets ( next_input ) != NULL ); 末位的分号不要
解决方案3: win 下 Ctrl+Z , linux 下 Ctrl+D .
以上介绍了“ 如何从键盘输入得到 gets next_input == NULL ?”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2914983.html