The programme fails if the string is teminated by space characters. Anybody has any idea, what should be done? Thanks in advance


#1

int lengthOfLastWord(const char* A) {
int i=0,count=0;
while((A+i)!=’\0’){
if (
(A+i)==’ ') count=0;
else count++;
i++;
}
if(i==0) return 0;
else return count;
}

// If the string “Hello World” is passed, the code works fine. If the string "Hello World " is passed, then the code gives length as 0 whereas the expected output is 5.


#2

Use trim method before processing to remove spaces before and after the string.