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

Comments: 2

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

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

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 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

profile
@sciaku1
11-Jan-2024, 03: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 (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?