#include <stdio.h>
#include <string.h>void main()
{
char str[100], *pStr, *EndStr;
unsigned int count=0; while (1)
{
pStr = gets(str);
if ( strlen(pStr) == 0 )
break;
else
if (strlen(pStr) > count)
{
count = strlen(pStr);
EndStr = pStr;
}
}
printf("%s\n", EndStr);
}为什么结果没是空的呢??请高手指点!

解决方案 »

  1.   

    呵呵,你的endstr和pstr指的是一个共享的字符穿。后来就覆盖了。
    #include <stdio.h>
    #include <string.h>
    void main()
    {
    char str[100], *pStr, *EndStr;
    unsigned int count=0; while (1)
    {
    pStr = gets(str);
    if ( strlen(pStr) == 0 )
    break;
    else
    if (strlen(pStr) > count)
    {
    count = strlen(pStr);
    EndStr=new char[count];
    strcpy(EndStr,pStr);
    }
    }
    printf("%s\n",EndStr);
    }
    这样就可以了
      

  2.   

    #include <stdio.h>
    #include <string.h>
    void main()
    { char str[100], *pStr, *EndStr;
    unsigned int count=0; while (1)
    {
    pStr = gets(str);
    EndStr="\n";
    if ( strlen(pStr) == 0 )
    break;
    else
    if (strlen(pStr) > count)
    {
    count = strlen(pStr);
    EndStr=new char[count];
    strcpy(EndStr,pStr);
    }
    }
    printf("%s\n",EndStr);}
      

  3.   

    怎么大家都 new 怎么没看见 delete 呢??
      

  4.   

    如果我连续输入10行(或更多),每次不是重新 new 空间??如果不 delete ,那不是输入越多行,程序就占用越多空间????
      

  5.   

    #include <stdio.h>
    #include <string.h>
    void main()
    {
    char str[100], *pStr, EndStr[1000];
    unsigned int count=0; while (1)
    {
    pStr = gets(str);
    if ( strlen(pStr) == 0 )
    break;
    else
    if (strlen(pStr) > count)
    {
    count = strlen(pStr);

    strcpy(EndStr,pStr);
    }
    }
    printf("%s\n",EndStr);
    }