qberik

Untitled

Nov 18th, 2021 (edited)
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #define SIZE 100
  6.  
  7. int main(){
  8.  
  9.   char words[SIZE][SIZE] = {};
  10.   int  word_count[SIZE] = {};
  11.   int word_size = 0;
  12.  
  13.   char str[SIZE] = "ket asjkvsls lol bruh bruh bruh kek lol";
  14.  
  15.   int last_word_end = -2;
  16.  
  17.   for( int i = 0; str[i] != '\0'; i++ ){
  18.  
  19.     if( str[i + 1] == ' ' || str[i + 1] == '\0' ){
  20.  
  21.      
  22.       int word_index = -1;
  23.       for( int j = 0; j < word_size; j++ ){
  24.         bool flag = true;
  25.         for( int k = 0; words[j][k] != '\0'; k++ )
  26.             if( str[last_word_end + 2 + k ] != words[j][k] )
  27.               flag = false;
  28.         if( flag )
  29.           word_index = j;
  30.       }
  31.  
  32.  
  33.       if( word_index != -1 ){
  34.         word_count[ word_index ] ++;      
  35.       }else{
  36.  
  37.         for( int j = 0; str[last_word_end+2+j]!=' ' && str[last_word_end+2+j]!='\0'; j++ ){
  38.           words[word_size][j] = str[last_word_end+2+j];
  39.         }
  40.         word_count[word_size] ++;
  41.         word_size++;
  42.       }
  43.  
  44.       last_word_end = i;
  45.     }
  46.  
  47.   }
  48.  
  49.   /* сюда */
  50.  
  51.  
  52.   for( int i = 0; i < word_size; i++ ){
  53.  
  54.     int max = 0;
  55.     int max_index = -1;
  56.     for( int j = i; j < word_size; j++ ){
  57.       if( word_count[ j ] > max ){
  58.         max = word_count[ j ];
  59.         max_index = j;  
  60.       }
  61.     }
  62.  
  63.     int tmp_int = word_count[i];
  64.     word_count[i] = word_count[max_index];
  65.     word_count[max_index] = tmp_int;
  66.  
  67.     char tmp_str[SIZE] = {};
  68.  
  69.     char *s1 = tmp_str, *s2 = words[i], *s3 = words[max_index];
  70.     while( *s1++ = *s2++ );
  71.     s2 = words[i];
  72.     while( *s2++ = *s3++ );
  73.     s3 = words[max_index]; s1 = tmp_str;
  74.     while( *s3++ = *s1++ );
  75.     /*
  76.     for( int j = 0; words[i][j] != '\0'; j++ )
  77.       tmp_str[j] = words[i][j];
  78.  
  79.     for( int j = 0; words[max_index][j] != '\0'; j++ )
  80.       words[i][j] = words[max_index][j];
  81.    
  82.  
  83.     for( int j = 0; tmp_str[j] != '\0'; j++ )
  84.       words[max_index][j] = tmp_str[j];*/
  85.   }
  86.  
  87.   for( int i = 0; i < word_size; i++ ){
  88.     cout << words[i] << ':' << word_count[i] << endl;
  89.   }
  90.  
  91.  
  92.   return 0;
  93. }
  94.  
Add Comment
Please, Sign In to add comment