C/C++

Лабораторная работа c++

Опять обращаюсь к всесильным майлрушникам), помогите пожалуйста, знаю что это не сложно, но у меня уже котелок не варит.




int main() {

string
maxword = "",
line,
buf;

ifstream file("text.txt");
while (!file.eof()) {
file >> line;
line += " ";
for (int i = 0, length = line.length(); i < length; i++) {
if (tolower(line[i]) >= 'a' && tolower(line[i]) <= 'z')
buf += line[i];
else {
if (maxword.length() < buf.length())
maxword = buf;
buf.clear();
}
}
}

ofstream out;
out.open("result.txt");
if (out.is_open())
{
out << maxword << std::endl;
}
cout << " Max length word : " << maxword << endl;
return 0;
}




Вот мой вариант, но проблема в том что нельзя использовать string, только char
#include <iostream>
#include <fstream>
#include <clocale>
#include <cstring>
#include <cstdlib>
using namespace std;
int main() {
char line[0xFFFF];
ifstream inp("text.txt");
inp.getline(line, size(line));
char long_word[0xFF] = { 0 };
const char* delim = " ";
char* word = NULL;
char* context = NULL;
word = strtok_s(line, delim, &context);
strcpy_s(long_word, _countof(long_word), word);
while (word != NULL) {
word = strtok_s(NULL, delim, &context);
if (word && strlen(long_word) < strlen(word)) {
strcpy_s(long_word, _countof(long_word), word);
}
}
setlocale(LC_CTYPE, ".1251");
cout << "Самое длинное слово: ";
setlocale(LC_CTYPE, ".20866");
cout << long_word << '\n';
system("pause > nul");
}
Азамат Оспанов
Азамат Оспанов
86 321
Лучший ответ
Asker .... Маловато как-то библиотек. Не впечатляет )
Asker .... А 0xFF это у программистов что то вроде мантры, чтобы не забыть 16ричный формат? 255 не катит? (надо же придолбаться)
Asker .... Жена - купи десяток яиц. А? Десяток! А? )
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>

using namespace std;

int main() {
const int size = 100;

char
maxword[size],
line[size],
buf[size];
maxword[0] = '\0';

ifstream file("text.txt");
while (!file.eof()) {
file >> line;
int len_file = strlen(line);
line[len_file]= ' ';
line[len_file+1] = '\0';
int j = 0;
for (int i = 0, length = strlen(line); i < length; i++) {
if (tolower(line[i]) >= 'a' && tolower(line[i]) <= 'z') {
buf[j++]= line[i];
buf[j] = '\0';
} else {
if (strlen(maxword) < strlen(buf))
strcpy(maxword,buf);
buf[0] = '\0';
j = 0;
}
}
}

ofstream out;
out.open("result.txt");
if (out.is_open())
{
out << maxword << std::endl;
}
cout << " Max length word : " << maxword << endl;
return 0;
}