daily pastebin goal
86%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define N 20
  6.  
  7.  
  8. int comp (const void* str1, const void* str2) {
  9.  
  10.     return (strcmp(*(char**)str1, *(char**)str2));
  11. }
  12.  
  13.  
  14. int count (char* text) {
  15.    
  16.     int mem = N, i = 1, len, counter = 1, max = 1;
  17.  
  18.     char** array = (char**)malloc(mem*sizeof(char*));
  19.  
  20.     array[0] = strtok(text," ");
  21.     while (array[i-1] != NULL) {
  22.  
  23.         array[i] = strtok(NULL," ");
  24.         i++;
  25.  
  26.         if (mem == i) {
  27.  
  28.         mem += mem;
  29.         realloc(array, mem*sizeof(char*));
  30.         }
  31.     }
  32.  
  33.     len = i-1;
  34.    
  35.  //   for ( i=0; i < len; i++) {
  36.        
  37.  //       printf("%s\n", array[i]);
  38.  //   }
  39.     i = 0;
  40.    
  41.     if (!len) {
  42.        
  43.         return 0;
  44.     }
  45.     qsort(array, len, sizeof(char*), comp);
  46.    
  47.  
  48.     while ( i < len-1 ){
  49.        
  50.         i++;
  51.         if ( !strcmp(array[i], array[i-1]) ) {
  52.            
  53.             counter++;
  54.         }
  55.         //printf("%d", len);
  56.         if ( strcmp(array[i], array[i-1]) || i == len - 1) {
  57.            
  58.             if (max < counter) {
  59.                
  60.                 max = counter;
  61.                 counter = 1;
  62.             }
  63.         }
  64.     }
  65.     return max;
  66. }
  67.  
  68. int main() {
  69.  
  70.     int n = 0;
  71.    
  72.     char* text = (char*) malloc (201*sizeof(char));
  73.     fgets(text, 201, stdin);
  74.        
  75.     n = count(text);
  76.     printf("%d", n);
  77.  
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top