Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4. #include <iterator>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. void trim(string &str) {
  10.     string::iterator b = str.begin();
  11.     while (isspace(*b)) {
  12.         b++;
  13.     }
  14.  
  15.     string::iterator e = str.end();
  16.     while (isspace(*(e - 1))) {
  17.         e--;
  18.     }
  19.  
  20.     str.assign(b, e);
  21. }
  22.  
  23. int main()
  24. {
  25.  
  26.     char str[200];
  27.     cin.getline(str, 200);
  28.     char *pch = strtok (str, " "), *word = 0;
  29.     int length = strlen(pch);
  30.     int maxLen = 0;
  31.     string ss = string(str);
  32.  
  33.     trim(ss);
  34.  
  35. strcpy(str, ss.c_str() );
  36.  
  37.    while (pch != NULL)
  38.       {
  39.           length = strlen(pch);
  40.  
  41.         if (maxLen < length)
  42.           {
  43.               maxLen = length;
  44.               word = pch;
  45.           }
  46.           pch = strtok (NULL, " ");
  47.       }
  48.  
  49.       cout <<  word << endl;
  50.       cout << maxLen;
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement