Advertisement
Caneq

lb5.3.8

Feb 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. char* strtoc(char* st = 0, char* delim = 0) {
  6.     static char* str = st;
  7.     static char* delims = delim;
  8.     static int len = strlen(str) + 1;
  9.     static int pos = 0;
  10.     static int dlen = strlen(delim) + 1;
  11.     if (st) {
  12.         str = st;
  13.         pos = 0;
  14.         len = strlen(str) + 1;
  15.     }
  16.     int i = pos;
  17.  
  18.     if (delim) {
  19.         delims = delim;
  20.         dlen = strlen(delim) + 1;
  21.     }
  22.  
  23.     for (; i < len; i++) {
  24.         for (int j = 0; j <= dlen; j++) {
  25.             if (delims[j] == str[i]) {
  26.                 str[i] = 0;
  27.                 char* word = str + pos;
  28.                 pos = i + 1;
  29.                 for (int j = 0; j < dlen; j++) {
  30.                     if (word[0] == delims[j]) {
  31.                         return strtoc();
  32.                     }
  33.                 }
  34.                 return word;
  35.             }
  36.         }
  37.     }
  38.     return 0;
  39. }
  40. char** split(char* s, char* delim, int* lenres) {
  41.     int lenre = 0;
  42.     int len = strlen(s) + 1;
  43.     int dlen = strlen(delim) + 1;
  44.     char* stemp = new char[len];
  45.     strcpy_s(stemp, len, s);
  46.  
  47.     char* word = strtoc(stemp, delim);
  48.     int maxlen = 0;
  49.     int temp;
  50.     if (word) {
  51.         lenre++;
  52.         temp = strlen(word);
  53.         if (temp > maxlen) maxlen = temp;
  54.     }
  55.     while (true) {
  56.         word = strtoc();
  57.         if (!word) break;
  58.         temp = strlen(word);
  59.         if (temp > maxlen) maxlen = temp;
  60.         lenre++;
  61.     }
  62.     delete stemp;
  63.  
  64.     char** splited = new char*[lenre];
  65.     for (int j = 0; j < lenre; j++) {
  66.         splited[j] = new char[maxlen + 1];
  67.     }
  68.  
  69.     word = strtoc(s, delim);
  70.     int i = 0;
  71.     if (word) {
  72.         strcpy_s(splited[i], maxlen + 1, word);
  73.     }
  74.     i++;
  75.     while (true) {
  76.         word = strtoc();
  77.         if (!word) break;
  78.         strcpy_s(splited[i], maxlen + 1, word);
  79.         i++;
  80.     }
  81.     *lenres = lenre;
  82.     return splited;
  83. }
  84. void sortWords(char** words, int n) {
  85.     for (int i = 0; i < n - 1; i++){  
  86.         int min = words[i][0];
  87.         int index_min = i;
  88.         for (int j = i + 1; j < n; j++) {
  89.             if (words[j][0] < min) {
  90.                 min = words[j][0];
  91.                 index_min = j;
  92.             }
  93.         }
  94.         char* temp = words[index_min];
  95.         words[index_min] = words[i];
  96.         words[i] = temp;
  97.     }
  98.  
  99. }
  100. void printWords(char** words, int n) {
  101.     for (int i = 0; i < n; i++) {
  102.         cout << words[i] << endl;
  103.     }
  104. }
  105.  
  106. int main(){
  107.     const int n = 500;
  108.     char delims[] = " .,?!:;'\"/\\";
  109.     char* st = new char[n];
  110.     cin.getline(st, n);
  111.     int len = 0;
  112.     char** words = split(st, delims, &len);
  113.     sortWords(words, len);
  114.     printWords(words, len);
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement