求助!!!

Foresee 2011-12-01 01:28:01
读入若干字符行,以空行(即只键入回车符的行)结束,输出其中最长的行。
...全文
71 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
herocxgood 2011-12-01
  • 打赏
  • 举报
回复
char p[30]="abc";
char pt[40]="";
cout<<"please input the string:";
while(p[0])
{
//cin.getline(p,30);
if( strlen(p)>strlen(pt))
{
strcpy(pt,p);
}
//strcpy(p,"");
cin.getline(p,30);
}
cout<<"the longest string is:";
cout<<pt<<endl;
我对c++也刚开始学习,上面功能很简单
qq120848369 2011-12-01
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char line[100];
int max_chars = 0;

// fgets can be finished with ctrl+D as well.
while (fgets(line, 100, stdin) != NULL)
{
// enter only
if (line[0] == '\n')
{
break;
}

printf("%s", line);

int line_chars = strlen(line) - 1;

if (line_chars > max_chars)
{
max_chars = line_chars;
}
}

printf("max_chars : %d \n", max_chars);

return 0;
}
Viking_Mei 2011-12-01
  • 打赏
  • 举报
回复
getline(cin, str)
然后判断str是否为空即可


#include <string>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
string str;
string maxStr;

long index = 0;

cout << "Please input strings to test, seperate with Enter:\n" << index++ << "\t";

while ( getline(cin, str) ) {
// whether have input a empty line
if (str.empty()) {
break;
}

cout << index++ << "\t";

if ( str.length()>maxStr.length()) {
maxStr = str;
}
}

if (maxStr.empty()) {
cout << "\nSorry, you have not input any string!\n";
}
else {
cout << "\nThe longest string is: " << maxStr << endl;
}

return 0;
}
Foresee 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]
while循环呗
[/Quote]能不能详细点,最好给出程序
Foresee 2011-12-01
  • 打赏
  • 举报
回复
以空行(即只键入回车符的行)结束 这个怎么实现
bdmh 2011-12-01
  • 打赏
  • 举报
回复
while循环呗

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧