First Occurrence of Character
EasyPractice library · +10 XPstringsstring scanningloops
Given a string $S$ and a character $C$, find the 0-based index of the first occurrence of character $C$ in string $S$. If character $C$ does not appear in string $S$, output -1.
Input
The first line contains the string $S$. The second line contains the character $C$.
Output
Print a single integer: the 0-based index of the first occurrence of $C$ in $S$, or -1 if $C$ is not found.
Constraints
1 <= |S| <= 1000, where |S| is the length of the string $S$. $C$ is a single ASCII character.
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
hello world o
Output
4
The character 'o' first appears at index 4.
Example 2
Input
programming z
Output
-1
The character 'z' does not appear in the string, so -1 is printed.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
hello world o
Example 2 input
programming z