forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
length_of_last_word.cpp
49 lines (43 loc) · 1.18 KB
/
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
43
44
45
46
47
48
49
/*
Given a string s consists of some words separated by spaces,
return the length of the last word in the string.
If the last word does not exist, return 0.
A word is a maximal substring consisting of non-space characters only.
*/
#include <bits/stdc++.h>
using namespace std;
/*
Well, the basic idea is very simple.
Start from the tail of s and move backwards to find the first non-space character.
Then from this character, move backwards and count the number of non-space characters
until we pass over the head of s or meet a space character.
The count will then be the length of the last word.
*/
int lengthOfLastWord(string s)
{
int lengthofword = 0, tail = s.length() - 1;
while (tail >= 0 && s[tail] == ' ')
tail--;
while (tail >= 0 && s[tail] != ' ')
{
lengthofword++;
tail--;
}
return lengthofword;
}
int main()
{
cout << "Enter the string:";
string str;
getline(cin, str);
int ans = lengthOfLastWord(str);
cout << "The length of the last word in the string is: " << ans;
}
/*
Input:Hello World
Output:5
*/
/*
Time Complexity:O(n),where n is the length of the string
Space Complexity:O(1)
*/