Mira2706

hw2_pointers

Feb 2nd, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. char* longest_word(char *phrase)
  6. {
  7.     int counter = 0;
  8.     int max_length = 0;
  9.     int index = 0;
  10.     for(int i = 0; phrase[i] != '\0'; i++)
  11.     {
  12.         if(phrase[i] != ' ')
  13.         {
  14.            counter++;
  15.         }
  16.         else if (counter > max_length)
  17.         {
  18.             max_length = counter;
  19.  
  20.             index = i ;
  21.             counter = 0;
  22.         }
  23.  
  24.     }
  25.     return phrase + index;
  26. }
  27.  
  28. int main()
  29. {
  30.     char phrase[20];
  31.     cin>>phrase;
  32.     cin.getline(phrase, 20);
  33.     cout<<longest_word(phrase);
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment