fork(3) download
  1. #include <iostream>
  2. #include <cctype>
  3.  
  4. const char* max_word(const char* s, const char*& e, int& n){
  5. int k, l, i;
  6. const char* p, *m, *t = s;
  7.  
  8. m = NULL;
  9. i = 1;
  10. for(n = k = 0; *s; ++s){
  11. if(std::isalpha(*s) && ! std::isalpha(*(s + 1))){
  12. p = s - 1;
  13. while((p >= t) && std::isalpha(*p))
  14. --p;
  15. ++p;
  16. l = (int)(s - p);
  17. if(l > k){
  18. k = l;
  19. n = i;
  20. m = p;
  21. e = s + 1;
  22. }
  23. ++i;
  24. }
  25. }
  26. return m;
  27. }
  28.  
  29.  
  30. int main(void){
  31. char s[] = "begin: saturn, [jupiter], neptun, uran.";
  32.  
  33. int n;
  34. const char* e;
  35. const char* w = max_word(s, e, n);
  36. if(w != NULL){
  37. std::cout << "word: ";
  38. std::cout.write(w, (int)(e - w));
  39. std::cout << std::endl;
  40.  
  41. std::cout << "index: " << (size_t)(w - s) << std::endl;
  42. std::cout << "number: " << n << std::endl;
  43. std::cout << "substr: " << w << std::endl;
  44. }
  45. return 0;
  46. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
word:   jupiter
index:  17
number: 3
substr: jupiter], neptun, uran.