Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- int main() {
- const short int length = 80;
- char str[81] = "";
- char words[40][21] = {};
- int words_length[length] = {};
- gets_s(str, length);
- // Получаем массив с выделенными словами и введенного предложения
- char seps[] = "!'\";:?-., "; // строка разделителей
- // Копирование исходной строки, чтобы избежать изменения исходной строки
- char copy_str[81] = "";
- strcpy(copy_str, str);
- char* token; // ссылка на начало подстроки
- token = strtok(copy_str, seps); // выделение первой подстроки
- int count_words = 0;
- while(token != NULL)
- {
- char word[21] = "";
- strcpy(word, token); // Выделяем слово
- // Проверяем, содержит ли слово недопустимый символ
- int is_ok = 1;
- char *symbol = word;
- while (*symbol != '\0' && is_ok)
- {
- is_ok = isalnum(symbol[0]);
- symbol++;
- }
- if (is_ok)
- // Если слово не содержит недопустимый символ, то добавляем его в массив слов
- strcpy(words[count_words++], word);
- token = strtok(NULL, seps); // выделение следующей подстроки
- }
- // Сортируем массив со словами в порядке убывания их длин
- for (int i = 0; i < count_words - 1; i++)
- {
- for (int j = 0; j < count_words - 1 - i; j++)
- {
- if (strlen(words[j]) < strlen(words[j + 1]))
- {
- char tmp[21] = ""; // буфер для обмена словами
- strcpy(tmp, words[j]);
- strcpy(words[j], words[j + 1]);
- strcpy(words[j + 1], tmp);
- }
- }
- }
- // Печатаем результат в консоль
- for (int i = 0; i < count_words; i++)
- {
- printf("%s", words[i]);
- printf("\n");
- }
- if (!count_words)
- printf("no solution");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement