Advertisement
Caneq

lb4.3.8

Nov 25th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include "pch.h"
  2. #include <string.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     const int MAX = 4000;
  9.     char str[MAX];
  10.     char new_str[MAX] = { 0 };
  11.  
  12.     const char DELIMS[] = " .,:;!?-\'()";
  13.     char *word, *Ntr;
  14.  
  15.     cout << "Enter a string: ";
  16.     cin.getline(str, MAX);
  17.  
  18.     word = strtok_s(str, DELIMS, &Ntr);
  19.  
  20.     while (word != NULL) {
  21.         int cnt_no_repeat = 0;
  22.         int cnt_repeat = 0;
  23.         int ptr_len = strlen(word) + 1;
  24.  
  25.         char *ptr_temp = new char[ptr_len];
  26.         strcpy_s(ptr_temp, ptr_len, word);
  27.  
  28.         for (int i = 0; ptr_temp[i]; i++) {
  29.             if (ptr_temp[i] == ' ') continue;
  30.             char ch_tmp = ptr_temp[i];
  31.             ptr_temp[i] = ' ';
  32.             char *b = strchr(ptr_temp, ch_tmp);
  33.             if (b != NULL) {
  34.                 cnt_repeat++;
  35.                 for (int j = b - ptr_temp; ptr_temp[j]; j++) {
  36.                     if (ptr_temp[j] == ch_tmp)
  37.                         ptr_temp[j] = ' ';
  38.                 }
  39.             }
  40.             else cnt_no_repeat++;
  41.         }
  42.         if (cnt_repeat > cnt_no_repeat) {
  43.             strcat_s(new_str, word);
  44.             int len = strlen(new_str);
  45.             new_str[len] = ' ';
  46.             new_str[len + 1] = 0;
  47.         }
  48.         word = strtok_s(NULL, DELIMS, &Ntr);
  49.     }
  50.     cout << endl << new_str << endl;
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement