fork(1) download
  1. #include <bits/stdc++.h>
  2. #include <string>
  3. using namespace std;
  4.  
  5. string LongestWord(string sen) {
  6.  
  7. // code goes here
  8. string s=sen;
  9. int count=0;
  10. int max1=0;
  11. int j=0;
  12. int k=0;
  13. for(int i=0;i<s.size();i++){
  14. if(s[i]==' '){
  15. if(count>max1){
  16. max1=count;
  17. j=k;
  18. k=i;
  19. }
  20. count=0;
  21. continue;
  22. }
  23. if(isalpha(s[i])){
  24. //cout<<s[i];
  25. count=count+1;
  26. }
  27. }
  28.  
  29. if(count>max1){
  30. max1=count;
  31. s=s.substr(k,sen.size()-1);
  32. }
  33. else{
  34. s=s.substr(j,k);
  35. }
  36. return s;
  37.  
  38. }
  39.  
  40. int main(void) {
  41.  
  42. // keep this function call here
  43. string s;
  44. cin>>s;
  45. cout << LongestWord(s);
  46. return 0;
  47.  
  48. }
Success #stdin #stdout 0.01s 5500KB
stdin
this is some sort of sentence
stdout
thi