Find the Index of the First Occurrence in a String In C,CPP,JAVA,PYTHON,C#,JS

Spread the love
Close-up of HTML code highlighted in vibrant colors on a computer monitor.

Problem Statement:

Given a string s and a target character ch, your task is to find the index of the first occurrence of the character ch in the string s. If the character is not found, return -1.

Input:

  • A string s of length n (1 ≤ n ≤ 10^5).
  • A character ch which is present in the string.

Output:

  • The index of the first occurrence of the character ch in the string s.
  • If the character is not found, return -1.

Example:

Example 1:

Input: s = "hello", ch = 'e'
Output: 1
Explanation: The first occurrence of 'e' is at index 1.

Example 2:

Input: s = "hello", ch = 'a'
Output: -1
Explanation: 'a' is not present in the string.

Approach:

To solve the problem, we can use a linear scan of the string s to look for the character ch. The first occurrence of ch in the string will be returned as the index. If no match is found, return -1.

  • Step 1: Iterate through the string.
  • Step 2: Check each character to see if it matches ch.
  • Step 3: If a match is found, return the current index.
  • Step 4: If the loop completes without finding the character, return -1.

Algorithm:

  1. Loop through each character of the string s from index 0 to n-1.
  2. For each character at index i, check if it is equal to ch.
  3. If a match is found, return the index i.
  4. If the loop finishes without finding the character, return -1.

Code Implementation:

C:

#include <stdio.h>
#include <string.h>

int first_occurrence(char* s, char ch) {
for (int i = 0; i < strlen(s); i++) {
if (s[i] == ch) {
return i;
}
}
return -1;
}

int main() {
char s[] = "hello";
char ch = 'e';
int result = first_occurrence(s, ch);
printf("%d\n", result); // Output: 1
return 0;
}

C++:

#include <iostream>
#include <string>
using namespace std;

int first_occurrence(string s, char ch) {
for (int i = 0; i < s.length(); i++) {
if (s[i] == ch) {
return i;
}
}
return -1;
}

int main() {
string s = "hello";
char ch = 'e';
cout << first_occurrence(s, ch) << endl; // Output: 1
return 0;
}

Java:

public class Main {
public static int firstOccurrence(String s, char ch) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ch) {
return i;
}
}
return -1;
}

public static void main(String[] args) {
String s = "hello";
char ch = 'e';
System.out.println(firstOccurrence(s, ch)); // Output: 1
}
}

Python:

def first_occurrence(s, ch):
for i in range(len(s)):
if s[i] == ch:
return i
return -1

s = "hello"
ch = 'e'
print(first_occurrence(s, ch)) # Output: 1

C#:

using System;

class Program {
static int FirstOccurrence(string s, char ch) {
for (int i = 0; i < s.Length; i++) {
if (s[i] == ch) {
return i;
}
}
return -1;
}

static void Main() {
string s = "hello";
char ch = 'e';
Console.WriteLine(FirstOccurrence(s, ch)); // Output: 1
}
}

JavaScript:

function firstOccurrence(s, ch) {
for (let i = 0; i < s.length; i++) {
if (s[i] === ch) {
return i;
}
}
return -1;
}

let s = "hello";
let ch = 'e';
console.log(firstOccurrence(s, ch)); // Output: 1

Time Complexity:

  • The algorithm loops through the string once, checking each character.
  • Time Complexity: O(n), where n is the length of the string s.

Space Complexity:

  • The space complexity is O(1) since we are not using any additional data structures that depend on the size of the input.

Edge Cases:

  1. Character not found: If the character ch is not in the string, the function should return -1.
  2. Empty string: If the string is empty, the function should return -1 as there is no character to search.
  3. Single character string: If the string consists of only one character, it should return 0 if the character matches.