скриншот:
Скриншот фрагмента кода удалён модератором.
Ниже написанный код.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int longestWord(char* str, char* longest_word) {
int len = strlen(str);
int max_len = 0, word_len = 0, word_start = 0;
for (int i = 0; i <= len; i++) {
if (str[i] == ' ' || str[i] == '\0') {
word_len = i - word_start;
if (word_len > max_len) {
max_len = word_len;
strncpy(longest_word, str + word_start, max_len);
}
word_start = i + 1;
}
}
return max_len;
}
void wordsOfSameLength(char* str, int len) {
int str_len = strlen(str);
int word_len = 0, word_start = 0;
cout << "Words of same length as the longest word:" << endl;
for (int i = 0; i <= str_len; i++) {
if (str[i] == ' ' || str[i] == '\0') {
word_len = i - word_start;
if (word_len == len) {
for (int j = word_start; j < i; j++) {
cout << str[j];
}
cout << endl;
}
word_start = i + 1;
}
}
}
int main() {
char filename[] = "input.txt";
ifstream fin(filename);
if (!fin.is_open()) {
cout << "Could not open file " << filename << endl;
return 1;
}
char str[100];
fin.getline(str, 100);
fin.close();
char longest_word[100];
int max_len = longestWord(str, longest_word);
cout << "Longest word: " << longest_word << ", length: " << max_len << endl;
wordsOfSameLength(str, max_len);
return 0;
}