-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastwordlength.cpp
45 lines (41 loc) · 1005 Bytes
/
lastwordlength.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
43
44
45
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
class Solution {
public:
int lengthOfLastWord(const char *s) {
int index1 = 0;
int index3 = 0;
int cnt = 0;
/* skip all space(if any) at the beginning of the string. */
while (s[index1] == ' ') {
index1++;
}
/* void string. */
if (s[index1] == '\0')
return 0;
/* delete the spaces at the end of word. */
while(s[index3] !='\0') {
index3++;
}
index3--;
while (s[index3] == ' ') {
std::cout << "_____" << endl;
index3--;
}
std::cout << "index " << index3 << endl;
while (s[index3] !=' ' && index3 >= 0) {
cnt++;
index3--;
}
return cnt;
}
};
int main() {
char a[10] = "a";
cout << a[0] << endl;
cout << a[1] << endl;
Solution sol;
cout << sol.lengthOfLastWord(a) << endl;
}