-
Notifications
You must be signed in to change notification settings - Fork 0
/
Length_of_Last_Word.cpp
42 lines (38 loc) · 962 Bytes
/
Length_of_Last_Word.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*************************************************************************
> File Name: Length_of_Last_Word.cpp
> Author: QilongZhang
> Mail: Speknight4534@gmail.com
> Created Time: 2014年11月08日 星期六 23时42分31秒
************************************************************************/
#include <iostream>
#include <string>
using namespace std;
class Solution{
public:
int lengthOfLastWord(const char *s){
int ret = 0;
int len = 0;
bool mark = false;
while(*s != '\0'){
if(*s == ' ' && mark){
ret = len;
len = 0;
mark = false;
}
else if(*s != ' '){
mark = true;
++len;
}
++s;
}
if(mark){
ret = len;
}
return ret;
}
};
int main(){
Solution st;
cout<<st.lengthOfLastWord("Hello world")<<endl;
return 0;
}