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.
I bought this course, it worth it!
Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it
Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website
Quick answers to common questions about our courses, quizzes, and learning platform
SCIAKU Team please upload 1st video of TREE please please please, please