I'm getting error for the input "a". Error message is like this ================================================================= ==22==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000008f at pc 0x55a501284db8 bp 0x7ffd0c0b9450 sp 0x7ffd0c0b9440 READ of size 1 at 0x60200000008f thread T0 #2 0x7f6a233ee082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082) 0x60200000008f is located 1 bytes to the left of 2-byte region [0x602000000090,0x602000000092)
The code I have used is
int lengthOfLastWord(char *s){
int count = 0;
int len = strlen(s) -1;
int templen = len;
for (int i = len;s[i] == ' ';i--){
templen--;
}
for (int i = templen; (s[i] != ' ') && (i >= 0); i--){
count++;
}
return count;
}