Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <string.h>
- #include <locale.h>
- using namespace std;
- size_t getAmountWord(char* line){
- size_t amount = 0;
- for (size_t i = 0; line[i] != '.'; i++)
- amount++;
- return ++amount;
- }
- char** getWords(char* line, size_t amount){
- char** words = new char*[amount]();
- for (int count = 0; count < amount; count++)
- words[count] = new char[64];
- int index_one = 0;
- for (int i = 0; i < amount; i++)
- {
- if (line[i] == ' '|| line[i] == '.')
- {
- int index_two = i;
- int buffer;
- buffer = index_one;
- int counter = 0;
- for (int j = buffer; j < index_two; j++)
- {
- words[counter][j-index_one] = line[j];
- }
- counter++;
- index_one = i+1;
- if (line[i] == '.') break;
- }
- }
- return words;
- }
- int count_Words(char* line, size_t amount) {
- int index_one = -1;
- int counter_words = 0;
- for (size_t i = 0; i < amount; i++)
- {
- if (line[i] == ' '|| line[i] == '.') counter_words++;
- }
- return counter_words;
- }
- void sort(char **lines, size_t amount){
- for (size_t i = 0; i < amount-1; i++)
- for (size_t j = i+1; j<amount; j++)
- {
- if(strcmp(lines[i], lines[j]) > 0){
- char *tmp = lines[i];
- lines[i] = lines[j];
- lines[j] = tmp;
- }
- }
- }
- void print_words(char** line, size_t amount)
- {
- for (size_t i = 0; i < amount;i++) printf("%s", line[i]);
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- char** words;
- size_t number_of_letters;
- printf("сколько будет символов?\n");
- scanf_s("%d", &number_of_letters);
- char* sentence = new char[number_of_letters];
- getchar();
- printf("введите предложение:\n");
- fgets(sentence, number_of_letters, stdin);
- int counter_words;
- counter_words = count_Words(sentence, getAmountWord(sentence));
- words=getWords(sentence, getAmountWord(sentence));
- sort(words, counter_words);
- print_words(words, counter_words);
- for (size_t i = 0; i < counter_words;) {
- delete[] words[i];
- }
- delete[] words;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement