Advertisement
fferum

Untitled

May 29th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <string.h>
  5. #include <locale.h>
  6. using namespace std;
  7.  
  8.  
  9. size_t getAmountWord(char* line){
  10.     size_t amount = 0;
  11.     for (size_t i = 0; line[i] != '.'; i++)
  12.         amount++;
  13.     return ++amount;
  14. }
  15.  
  16. char** getWords(char* line, size_t amount){
  17.     char** words = new char*[amount]();
  18.     for (int count = 0; count < amount; count++)
  19.         words[count] = new char[64];
  20.     int index_one = 0;
  21.     for (int i = 0; i < amount; i++)
  22.     {
  23.         if (line[i] == ' '|| line[i] == '.')
  24.         {
  25.             int index_two = i;
  26.             int buffer;
  27.             buffer = index_one;
  28.             int counter = 0;
  29.                 for (int j = buffer; j < index_two; j++)
  30.                 {
  31.                     words[counter][j-index_one] = line[j];
  32.                 }
  33.             counter++;
  34.             index_one = i+1;
  35.             if (line[i] == '.') break;
  36.  
  37.         }
  38.     }
  39.     return words;
  40. }
  41. int count_Words(char* line, size_t amount) {
  42.     int index_one = -1;
  43.     int counter_words = 0;
  44.     for (size_t i = 0; i < amount; i++)
  45.     {
  46.         if (line[i] == ' '|| line[i] == '.') counter_words++;
  47.     }
  48.     return counter_words;
  49. }
  50.  
  51.  
  52.  
  53. void sort(char **lines, size_t amount){
  54.     for (size_t i = 0; i < amount-1; i++)
  55.         for (size_t j = i+1; j<amount; j++)
  56.         {
  57.             if(strcmp(lines[i], lines[j]) > 0){
  58.                 char *tmp = lines[i];
  59.                 lines[i] = lines[j];
  60.                     lines[j] = tmp;
  61.                 }
  62.         }
  63. }
  64.  
  65. void print_words(char** line, size_t amount)
  66. {
  67.     for (size_t i = 0; i < amount;i++) printf("%s", line[i]);
  68. }
  69.  
  70. int main()
  71. {
  72.     setlocale(LC_ALL, "Russian");
  73.     char** words;
  74.     size_t number_of_letters;
  75.     printf("сколько будет символов?\n");
  76.     scanf_s("%d", &number_of_letters);
  77.     char* sentence = new char[number_of_letters];
  78.     getchar();
  79.     printf("введите предложение:\n");
  80.     fgets(sentence, number_of_letters, stdin);
  81.     int counter_words;
  82.     counter_words = count_Words(sentence, getAmountWord(sentence));
  83.     words=getWords(sentence, getAmountWord(sentence));
  84.     sort(words, counter_words);
  85.     print_words(words, counter_words);
  86.    
  87.     for (size_t i = 0; i < counter_words;) {
  88.         delete[] words[i];
  89.     }
  90.     delete[] words;
  91.    
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement