Last Occurence Of A Char

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 95:- Last Occurence Of A Char

To find the last occurrence of a specific character in a given string, you can iterate through the string from right to left and stop when you encounter the desired character. Here's a Python function to achieve this:

pythonCopy code

def last_occurrence_of_char(s, char):    for i in range(len(s) - 1, -1, -1):        if s[i] == char:            return i    return -1 # Example usage: input_string = "Hello, World!" target_char = "o" index = last_occurrence_of_char(input_string, target_char) if index != -1:    print(f"The last occurrence of '{target_char}' is at index: {index}") else:    print(f"'{target_char}' not found in the string.")

In this example, we are finding the last occurrence of the character "o" in the string "Hello, World!" and printing its index. The output will be:

perlCopy code

The last occurrence of 'o' is at index: 8

Keep in mind that Python uses 0-based indexing, so the first character's index is 0, the second character's index is 1, and so on. If the character is not found in the string, the function will return -1 to indicate that the character was not present.

14. Week7 - Assignments

2 Comments

@mk.info.work
mk.info.work Feb 17, 2024 at 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

@na3744
na3744 Feb 23, 2024 at 2:52 AM

I bought this course, it worth it!

@mk.info.work
mk.info.work Nov 15, 2023 at 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

@sciaku1
sciaku1 Jan 11, 2024 at 3:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support