Home > Back-end >  Huawei OJ a topic, calculating the length of the string the last words, words separated by Spaces.
Huawei OJ a topic, calculating the length of the string the last words, words separated by Spaces.

Time:09-22

#include
# include
using namespace std;
Int main (int arg c, char * * argv) {

String STR.
Int count=0;
Getline (cin, STR);
For (int I=STR. The size () - 1; I> 0; - I)
{
If (STR [I]==' ')
{
break;
}
+ + count;
}
coutreturn 0;
}
Very depressed, why in OJ platform will pass?

CodePudding user response:

Only one word is wrong, instead I=len - 1; I>=0

CodePudding user response:

First of all, when there is only one word, it is a little one;
Secondly if your program does not analysis exist other Spaces after the last word;
Overall robustness is poor; Huawei OJ above, of course, might not consider so much, see the comments when little detours to solve the problem
  • Related