Advertisement
Guest User

1

a guest
Dec 12th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<ctype.h>
  5.  
  6. using namespace std;
  7.  
  8. const int LIM = 1024;
  9.  
  10. int is_sep(const char c){
  11.     return c == ' ' || c == '\n';
  12. }
  13.  
  14. char* getword(int *nstr){
  15.     char buf[LIM], c;
  16.     int wlen = 0;
  17.     for (c = getchar(); c == ' '; c = getchar());
  18.     if (c == '\n'){
  19.         *nstr = 1;
  20.         return NULL;
  21.     }
  22.     for (; !is_sep(c); c = getchar()){
  23.         buf[wlen++] = c;
  24.     }
  25.     *nstr = (c == '\n');
  26.     buf[wlen++] = '\0';
  27.     return strcpy(new char[wlen], buf);
  28. }
  29.  
  30. char getmaxch(const char *cstr){
  31.  
  32.     int ent[CHAR_MAX + 1];
  33.     for (int i = 0; i <= CHAR_MAX; ++i){
  34.         ent[i] = 0;
  35.     }
  36.  
  37.     int len = strlen(cstr), best = 0;
  38.     for (int i = 0; i < len; ++i){
  39.         ++ent[cstr[i]];
  40.         if (ent[best] < ++ent[cstr[i]]){
  41.             best = cstr[i];
  42.         }
  43.     }
  44.  
  45.     return best;
  46.  
  47. }
  48.  
  49. int main(){
  50.  
  51.     char *words[LIM];
  52.     size_t wcnt = 0;
  53.  
  54.     int eof = 0;
  55.     do {
  56.         char *buf = getword(&eof);
  57.         if (buf != NULL){
  58.             words[wcnt++] = buf;
  59.         }
  60.     } while (!eof);
  61.  
  62.     if (wcnt == 0){
  63.         return 0;
  64.     }
  65.  
  66.     for (int i = 0; i < wcnt; ++i){
  67.         printf("%c ", getmaxch(words[i]));
  68.         delete[] words[i];
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement