mình làm bài tìm từ dài nhất này chia thành 2 trường hợp là xét từ đầu tiên rùi sau đó mới xét các phía sau . trường hợp xét chữ đầu tiên thì đúng còn trường hợp kia thì nếu mún in ra kết quả phải thêm dấu cách sau từ đó thì mới in ra kết quả .
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<math.h>
#include<string.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char s[20];
int len;
do
{
cout << " nhap chuoi ki tu : ";
cin.getline(s, 50);
cout << " chuoi vua nhap ";
cout << "*" << s << "*" << endl;
// tìm từ dài nhất
len = strlen(s);
int a[100];
int na = 0;
if (s[0] != NULL && s[0] != 32) // xét độ dài từ đầu tiên
{
int dem = 1;
for (int i = 1; i < len; i++)
{
if (s[i] != 32)
{
dem++;
}
else
break;
}
a[na++] = dem;
}
for (int i = 1; i < len; i++) // xét độ dài những từ tiếp theo
{
if (s[i] != 32)
{
int dem = 1;
for (int j = i + 1; j < len; j++)
{
if (s[j] != 32)
{
dem++;
}
else break;
}
a[na++] = dem;
}
}
int max = a[0]; // tìm số từ dài nhất là bao nhiêu
for (int i = 1; i < na; i++)
{
if (max < a[i])
{
max = a[i];
}
}
if (s[0] != NULL &&s[0] != 32) // xét từ đầu tiên có thoã không
{
int dem = 1;
int min;
for (int i = 1; i < len; i++)
{
if (s[i] != 32)
{
dem++;
}
else
{
min = i ;
break;
}
}
if (dem == max)
{
for (int j = 0; j <= min; j++)
cout << s[j];
cout << endl;
}
}
for (int i = 1; i < len; i++) // xét những từ phía sau có thoả không
{
int min;
int dem = 0;
if (s[i] != 32)
{
dem++;
for (int j = i + 1; j < len; j++)
{
if (s[j] != 32)
dem++;
else
{
min = j;
break;
}
}
}
if (dem == max)
{
for (int z = i; z < min-1; z++)
cout << s[z];
cout << endl;
}
}
} while (_getch() != 27);
return 0;
}