Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<stdio.h>
- #include<string.h>
- #include<ctype.h>
- using namespace std;
- const int LIM = 1024;
- int is_sep(const char c){
- return c == ' ' || c == '\n';
- }
- char* getword(int *nstr){
- char buf[LIM], c;
- int wlen = 0;
- for (c = getchar(); c == ' '; c = getchar());
- if (c == '\n'){
- *nstr = 1;
- return NULL;
- }
- for (; !is_sep(c); c = getchar()){
- buf[wlen++] = c;
- }
- *nstr = (c == '\n');
- buf[wlen++] = '\0';
- return strcpy(new char[wlen], buf);
- }
- char getmaxch(const char *cstr){
- int ent[CHAR_MAX + 1];
- for (int i = 0; i <= CHAR_MAX; ++i){
- ent[i] = 0;
- }
- int len = strlen(cstr), best = 0;
- for (int i = 0; i < len; ++i){
- ++ent[cstr[i]];
- if (ent[best] < ++ent[cstr[i]]){
- best = cstr[i];
- }
- }
- return best;
- }
- int main(){
- char *words[LIM];
- size_t wcnt = 0;
- int eof = 0;
- do {
- char *buf = getword(&eof);
- if (buf != NULL){
- words[wcnt++] = buf;
- }
- } while (!eof);
- if (wcnt == 0){
- return 0;
- }
- for (int i = 0; i < wcnt; ++i){
- printf("%c ", getmaxch(words[i]));
- delete[] words[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement