Advertisement
venik2405

lab3_yap

Sep 29th, 2021
1,133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.00 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <locale.h>
  6. #include <stdlib.h>
  7.  
  8. #define separators " \n\t"
  9.  
  10. int count = 0;
  11.  
  12. void main() {
  13.     setlocale(LC_ALL, "Rus");
  14.     printf("Программа печатает все слова, отличные от последнего слова. Для каждой гласной буквы указать сколько раз она встречается в полученном предложении..\nПрограмма печатает все слова, отличные от последнего слова в таком порядке, чтобы последняя буква каждого слова совпадала с первой буквой следующего слова.");
  15.     printf("\n\nВведите строку(не более 100 символов):\n");
  16.     char* currentWord;
  17.     char* lastWord = NULL;
  18.     char c;
  19.     int numberOfDifferentWords = 0;
  20.     char sentance[100];
  21.     gets_s(sentance, 100);
  22.     char words[50][50];
  23.     int wordsAmount = 0;
  24.     char tempWord[50];
  25.     char vowels[6] = { 'a', 'e', 'i', 'o', 'u', 'y' };
  26.  
  27.     int x = 0, y = 0;
  28.     for (int i = 0; i <= strlen(sentance); i++) {
  29.         if (sentance[i] != '\0') {
  30.             if (sentance[i] != ' ') {
  31.                 words[x][y] = sentance[i];
  32.                 y++;
  33.             }
  34.             else {
  35.                 if (y > 0) {
  36.                     words[x][y] = '\0';
  37.                     y = 0;
  38.                     x++;
  39.                     wordsAmount++;
  40.                 }
  41.             }
  42.         }
  43.         else {
  44.             if (y > 0) {
  45.                 words[x][y] = '\0';
  46.             }
  47.             else
  48.                 x--;
  49.             wordsAmount--;
  50.         }
  51.     }
  52.     char flag = 1;
  53.     for (int i = 0; i <= x; i++) {
  54.         int j = 0;
  55.         flag = 1;
  56.         while (flag && words[i][j] != '\0') {
  57.             if (!(words[i][j] >= 65 && words[i][j] <= 90) && !(words[i][j] >= 97 && words[i][j] <= 122))
  58.                 flag = 0;
  59.             j++;
  60.         }
  61.         if (!flag) {
  62.             printf("%s - недопустимая комбинация символов\n", words[i]);
  63.             for (int z = i; z < x; z++)
  64.                 strcpy_s(words[z], words[z + 1]);
  65.             x--;
  66.             i--;
  67.         }
  68.     }
  69.  
  70.     lastWord = words[x];
  71.     printf("Все слова, отличные от последнего:\n");
  72.     for (int j = 0; j < x; j++) {
  73.         if (strcmp(lastWord, words[j]) != 0) {
  74.             numberOfDifferentWords++;
  75.             printf("  %s", words[j]);
  76.         }
  77.     }
  78.     if (numberOfDifferentWords == 0) {
  79.         printf("\tНет слов, отличных от последнего.\n\n");
  80.     }
  81. //    strcpy(words[x], "");
  82.  
  83.     if (numberOfDifferentWords != 0) {
  84.         for (int k = 0; k < 6; k++) {
  85.             count = 0;
  86.             for (int i = 0; i < x; i++) {
  87.                 for (int j = 0; i > -1 && j < strlen(words[i]); j++) {
  88.                     if (vowels[k] == words[i][j]) {
  89.                         count++;
  90.                     }
  91.                 }
  92.             }
  93.             printf("\n\t\tБуква %c встречается %d раз", vowels[k], count);
  94.         }
  95.     }
  96.  
  97.     for (int i = 0; i < x - 1; i++) {
  98.         for (int j = i + 1; j < x; j++) {
  99.             if (words[i][strlen(words[i]) - 1] == words[j][0]) {
  100.                 strcpy(tempWord, words[j]);
  101.                 strcpy(words[j], words[i + 1]);
  102.                 strcpy(words[i + 1], tempWord);
  103.             }
  104.             if (words[i][0] == words[j][strlen(words[j]) - 1]) {
  105.                 strcpy(tempWord, words[i + 1]);
  106.                 strcpy(words[i + 1], words[i]);
  107.                 strcpy(words[i], words[j]);
  108.                 strcpy(words[j], tempWord);
  109.             }
  110.         }
  111.     }
  112.  
  113.     if (numberOfDifferentWords != 0) {
  114.         printf("\n\tНовая строка\n\n");
  115.         for (int j = 0; j < x; j++) {
  116.             printf("  %s", words[j]);
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement