Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- using namespace std;
- char* strtoc(char* st = 0, char* delim = 0) {
- static char* str = st;
- static char* delims = delim;
- static int len = strlen(str) + 1;
- static int pos = 0;
- static int dlen = strlen(delim) + 1;
- if (st) {
- str = st;
- pos = 0;
- len = strlen(str) + 1;
- }
- int i = pos;
- if (delim) {
- delims = delim;
- dlen = strlen(delim) + 1;
- }
- for (; i < len; i++) {
- for (int j = 0; j <= dlen; j++) {
- if (delims[j] == str[i]) {
- str[i] = 0;
- char* word = str + pos;
- pos = i + 1;
- for (int j = 0; j < dlen; j++) {
- if (word[0] == delims[j]) {
- return strtoc();
- }
- }
- return word;
- }
- }
- }
- return 0;
- }
- char** split(char* s, char* delim, int* lenres) {
- int lenre = 0;
- int len = strlen(s) + 1;
- int dlen = strlen(delim) + 1;
- char* stemp = new char[len];
- strcpy_s(stemp, len, s);
- char* word = strtoc(stemp, delim);
- int maxlen = 0;
- int temp;
- if (word) {
- lenre++;
- temp = strlen(word);
- if (temp > maxlen) maxlen = temp;
- }
- while (true) {
- word = strtoc();
- if (!word) break;
- temp = strlen(word);
- if (temp > maxlen) maxlen = temp;
- lenre++;
- }
- delete stemp;
- char** splited = new char*[lenre];
- for (int j = 0; j < lenre; j++) {
- splited[j] = new char[maxlen + 1];
- }
- word = strtoc(s, delim);
- int i = 0;
- if (word) {
- strcpy_s(splited[i], maxlen + 1, word);
- }
- i++;
- while (true) {
- word = strtoc();
- if (!word) break;
- strcpy_s(splited[i], maxlen + 1, word);
- i++;
- }
- *lenres = lenre;
- return splited;
- }
- void sortWords(char** words, int n) {
- for (int i = 0; i < n - 1; i++){
- int min = words[i][0];
- int index_min = i;
- for (int j = i + 1; j < n; j++) {
- if (words[j][0] < min) {
- min = words[j][0];
- index_min = j;
- }
- }
- char* temp = words[index_min];
- words[index_min] = words[i];
- words[i] = temp;
- }
- }
- void printWords(char** words, int n) {
- for (int i = 0; i < n; i++) {
- cout << words[i] << endl;
- }
- }
- int main(){
- const int n = 500;
- char delims[] = " .,?!:;'\"/\\";
- char* st = new char[n];
- cin.getline(st, n);
- int len = 0;
- char** words = split(st, delims, &len);
- sortWords(words, len);
- printWords(words, len);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement