Advertisement
qberik

Untitled

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