Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #define SIZE 100
- int main(){
- char words[SIZE][SIZE] = {};
- int word_count[SIZE] = {};
- int word_size = 0;
- char str[SIZE] = "ket asjkvsls lol bruh bruh bruh kek lol";
- int last_word_end = -2;
- for( int i = 0; str[i] != '\0'; i++ ){
- if( str[i + 1] == ' ' || str[i + 1] == '\0' ){
- int word_index = -1;
- for( int j = 0; j < word_size; j++ ){
- bool flag = true;
- for( int k = 0; words[j][k] != '\0'; k++ )
- if( str[last_word_end + 2 + k ] != words[j][k] )
- flag = false;
- if( flag )
- word_index = j;
- }
- if( word_index != -1 ){
- word_count[ word_index ] ++;
- }else{
- for( int j = 0; str[last_word_end+2+j]!=' ' && str[last_word_end+2+j]!='\0'; j++ ){
- words[word_size][j] = str[last_word_end+2+j];
- }
- word_count[word_size] ++;
- word_size++;
- }
- last_word_end = i;
- }
- }
- /* сюда */
- for( int i = 0; i < word_size; i++ ){
- int max = 0;
- int max_index = -1;
- for( int j = i; j < word_size; j++ ){
- if( word_count[ j ] > max ){
- max = word_count[ j ];
- max_index = j;
- }
- }
- int tmp_int = word_count[i];
- word_count[i] = word_count[max_index];
- word_count[max_index] = tmp_int;
- char tmp_str[SIZE] = {};
- char *s1 = tmp_str, *s2 = words[i], *s3 = words[max_index];
- while( *s1++ = *s2++ );
- s2 = words[i];
- while( *s2++ = *s3++ );
- s3 = words[max_index]; s1 = tmp_str;
- while( *s3++ = *s1++ );
- /*
- for( int j = 0; words[i][j] != '\0'; j++ )
- tmp_str[j] = words[i][j];
- for( int j = 0; words[max_index][j] != '\0'; j++ )
- words[i][j] = words[max_index][j];
- for( int j = 0; tmp_str[j] != '\0'; j++ )
- words[max_index][j] = tmp_str[j];*/
- }
- for( int i = 0; i < word_size; i++ ){
- cout << words[i] << ':' << word_count[i] << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment