Skip to content

Instantly share code, notes, and snippets.

@IgorVodka
Created December 23, 2016 15:23
Show Gist options
  • Save IgorVodka/4e3d220720a1f1a4a23519a4f8aaf795 to your computer and use it in GitHub Desktop.
Save IgorVodka/4e3d220720a1f1a4a23519a4f8aaf795 to your computer and use it in GitHub Desktop.
#include "stdafx.h"
#include <iostream>
using namespace std;
#define STR_LEN 81
bool sameStrings(char* first, char* second, int firstLen, int secondLen) {
if (firstLen != secondLen)
return false;
for (int i = 0; i < firstLen; i++)
if (first[i] != second[i])
return false;
return true;
}
int main()
{
system("chcp 1251 > nul");
char* input = new char[STR_LEN];
char* fixedInput = new char[STR_LEN];
char* lastWord = new char[STR_LEN];
char* curWord = new char[STR_LEN];
int posFI = 0;
int posLW = 0;
int posCW = 0;
cout << "Введите текст (только латинские строчные буквы и пробелы, в конце - точка): " << endl;
cin.getline(input, STR_LEN);
while (*input && *input != '.') {
if (*input >= 'a' && *input <= 'z') {
fixedInput[posFI++] = *input;
lastWord[posLW++] = *input;
}
else if (*input == ' ') {
posLW = 0;
if (posFI > 0 && fixedInput[posFI - 1] != ' ')
fixedInput[posFI++] = ' ';
}
*input++;
}
fixedInput[posFI] = '\0';
bool needSpace = false;
cout << "Результат: ";
while (true) {
if (*fixedInput >= 'a') {
curWord[posCW++] = *fixedInput;
}
else {
if (!sameStrings(curWord, lastWord, posCW, posLW)) {
curWord[posCW] = '\0';
char* buf = curWord;
bool used[256] = { 0 };
if(needSpace)
cout << ' ';
while (*buf != '\0') {
if (*buf >= 'a' && *buf <= 'z') {
if (!used[*buf]) {
cout << *buf;
used[*buf] = true;
}
}
else if (*buf == ' ' && (*(buf - 1) != ' ')) {
cout << ' ';
memset(used, 0, 256 * sizeof used[0]);
}
buf++;
needSpace = true;
}
}
if (*fixedInput == '\0')
break;
posCW = 0;
}
*fixedInput++;
}
lastWord[posLW] = '\0';
system("pause > nul");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment