• Предмет: Информатика
  • Автор: polinakochergina06
  • Вопрос задан 5 месяцев назад

Дана последовательность символов, состоящая из слов. Удалить слова, которые содержат все повторяющиеся буквы первого слова, с сохранением структуры пробелов. Вывести на экран отредактированный текст или сообщение «Нет», если удаления слов не было. на си​

Ответы

Ответ дал: asilvejstruk
0

#include <stdio.h>

#include <string.h>

#include <ctype.h>

#define MAX_LEN 100

int main() {

   char text[MAX_LEN];

   char firstWord[MAX_LEN];

   int firstWordLen = 0;

   int textLen = 0;

   int isFirstWord = 1;

   int isDeleted = 0;

   // Reading input text

   printf("Enter a sequence of characters: ");

   fgets(text, MAX_LEN, stdin);

   textLen = strlen(text);

   // Finding the first word

   for (int i = 0; i < textLen; i++) {

       if (isalpha(text[i])) {

           if (isFirstWord) {

               firstWord[firstWordLen] = text[i];

               firstWordLen++;

           }

       } else {

           isFirstWord = 0;

       }

   }

   // Removing the words that contain all repeating letters of the first word

   for (int i = 0; i < textLen; i++) {

       if (isalpha(text[i])) {

           int j = 0;

           int k = i;

           int isDeletable = 1;

           while (isalpha(text[k]) && j < firstWordLen) {

               if (firstWord[j] != text[k]) {

                   isDeletable = 0;

                   break;

               }

               j++;

               k++;

           }

           if (isDeletable) {

               int l = i;

               while (isalpha(text[l])) {

                   for (int m = l; m < textLen; m++) {

                       text[m] = text[m + 1];

                   }

                   textLen--;

               }

               i--;

               isDeleted = 1;

           }

       }

   }

   // Printing the edited text

   if (isDeleted) {

       printf("Edited text: %s\n", text);

    } else {

       printf("No words were deleted.\n");

   }

   return 0;

}

Вас заинтересует