Skip to content

Instantly share code, notes, and snippets.

@nikoncode
Created September 16, 2014 06:05
Show Gist options
  • Select an option

  • Save nikoncode/e41bbde764ab87130859 to your computer and use it in GitHub Desktop.

Select an option

Save nikoncode/e41bbde764ab87130859 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <windows.h>
int main() {
//Set windows-1251 codepage
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
//Memory init
char * str = new char[100];
char * res = new char[100];
char * word_begin = str, *iterator, *iterator_two, *progress = res;
int count = 0;
//Reading
puts("Ââåäèòå ñòðîêó: ");
gets(str); //not-safe use fgets
//task
int length = strlen(str);
for (int i = 0; i <= length; ++i) {
iterator = str + i;
//word break
if (*iterator == ' ' || *iterator == '\0') {
//counting
for (iterator_two = word_begin; iterator_two < iterator; ++iterator_two) {
if (*iterator_two == *(iterator - 1)) {
count++;
}
}
//adding first symbols
while (count != 0) {
*progress = *(iterator - 1);
progress++;
count--;
}
//copying word
strncpy(progress, word_begin, iterator - word_begin + 1);
progress += iterator - word_begin + 1;
//move output ptr
word_begin = iterator + 1;
}
}
puts(res);
//free mem
delete [] str;
delete [] res;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment