![]() |
VOOZH | about |
Given a character ch and a string s, the task is to find the index of the first occurrence of the character in the string. If the character is not present in the string, return -1.
Examples:
Input: s = "geeksforgeeks", ch = 'k'
Output: 3
Explanation: The character 'k' is present at index 3 and 11 in "geeksforgeeks", but it first appears at index 3.Input: s = "geeksforgeeks", ch = 'z'
Output: -1
Explanation: The character 'z' is not present in "geeksforgeeks".
Table of Content
The idea is to traverse the input string s and for each character, check if it is equal to the character we are searching for. If we find a match, return the index of the current character.
If we reach the end of the string without finding any occurrence of the character, return -1.
3
We can also use inbuilt library functions to search for a character in a string. This makes the search simple and easier to implement.
3