65,210
社区成员
发帖
与我相关
我的任务
分享#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;
}
#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;
}