I want to find out the length of last word in a given string(a). A word is defined as a sequence of characters without a space(' '). The condition is not to use any of the library functions in doing this. I know its possible in C++. Can it be done in Java too? I did it using the following:
for(char c:a.toCharArray()) //this is not correct though. I need one without using an inbuilt method(considering its possible)
Is there a method that does not use the library functions?
edit:
Here is the solution in C++. Note it doesn't use a library function not even strlen()
at any point.
class Solution {
public:
int lengthOfLastWord(const string &s) {
int len = 0;
while (*s) {
if (*s != ' ') {
len++;
s++;
continue;
}
s++;
if (*s && *s != ' ') len = 0;
}
return len;
}};