Function lengthOfLastWord - CTS Pattern

 Coding Problem Keys 

 CTS Pattern 

 Function lengthOfLastWord 

 Problem Statement 

You are required to fix all the logical errors in the given code. You can click on run anytime to check the compilation/execution status of the program. You can use printf to debug your code. The submitted code should be logically/syntactically correct and pass all test cases. Do not write the main() function as it is not required.

Code Approach: For this question, you will need to correct the given implementation. We do not expect you to modify the approach or incorporate any additional library methods.

The function lengthOfLastWord (char *str, int len) accepts a string and length len as the input. The function return the count of last word in a given string.

The function compiles successfully but fails to print the desired result due to logical errors.

Your task is to debug the program to pass all the test cases.

 Max Execution Time Limit: 5000 millisecs 

 Given Code 

int lengthOfLastWord(char *str, int len){
    int count = 0;
    while(len > 0){
        count++;
        if(str[len] == ' '){
            continue;
        }
    }
    return count;
}

 Solution 

 Programming Language: C Language 

int lengthOfLastWord(char *str, int len){
    int count = 0;
    len--;
    while(len >= 0){
        if(str[len] == ' '){
            break;
        }
        len--;
        count++;
    }
    return count;
}

// Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Largest of Tenth Digit and Unit Digit

Same Unit Digit Adjacent Numbers

Jooney's Amusement Park

Christmas Tree Pattern

First N Repeated Characters

Print Grandson's Name