Cpp code without any library function

int Solution::lengthOfLastWord(const string A) {
    if(A[0]=='\0') return 0;
    int size=0,l=0,r=0;
    while(A[size] != '\0'){
        if(A[size-1]==' '&&A[size]!=' ') l= size;
        if((A[size]!=' '&&A[size+1]==' ')||(A[size]!=' '&&A[size+1] == '\0')) r= size+1;
    return r-l;


Bro, your code is accessing out of bound index of the string;
In starting you assigned size = 0 and in while loop you are checking A[size-1] means A[-1] , which is inaccesible, although your code worked in this compiler, but in most of compiler it ill give runtime exception. include condition of if(size-1>=0 && A[size-1]==’ ’ && A[size]!=’ ')… also for A[size+1].