Open In App

Program to Search a Character in a String

Last Updated : 25 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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".

Approach - By traversing the string - O(n) Time and O(1) Space

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.

C++
// C++ program to search a character in a string

#include <iostream>
using namespace std;

// function to find the first occurrence of ch in s
int findChar(string &s, char ch) {
    int n = s.length();
    for (int i = 0; i < n; i++) {

        // If the current character is equal to ch,
        // return the current index
        if (s[i] == ch)
            return i;
    }

    // If we did not find any occurrence of ch,
    // return -1
    return -1;
}

int main() {
    string s = "geeksforgeeks";
    char ch = 'k';

    cout << findChar(s, ch) << "\n";
    return 0;
}
C
// C program to search a character in a string

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

// function to find the first occurrence of ch in s
int findChar(char *s, char ch) {
    int n = strlen(s);
    for (int i = 0; i < n; i++) {
      
        // If the current character is equal to ch, 
        // return the current index
        if (s[i] == ch)
            return i;
    }

    // If we did not find any occurrence of ch,
    // return -1
    return -1;
}

int main() {
    char s[] = "geeksforgeeks";
    char ch = 'k';
  
    printf("%d\n", findChar(s, ch));
    return 0;
}
Java
// Java program to search a character in a string

class GfG {
  
    // function to find the first occurrence of ch in s
    static int findChar(String s, char ch) {
        int n = s.length();
        for (int i = 0; i < n; i++) {
          
            // If the current character is equal to ch, 
            // return the current index
            if (s.charAt(i) == ch)
                return i;
        }

        // If we did not find any occurrence of ch,
        // return -1
        return -1;
    }

    public static void main(String[] args) {
        String s = "geeksforgeeks";
        char ch = 'k';
      
        System.out.println(findChar(s, ch));
    }
}
Python
# Python program to search a character in a string

# function to find the first occurrence of ch in s
def findChar(s, ch):
    n = len(s)
    for i in range(n):
      
        # If the current character is equal to ch, 
        # return the current index
        if s[i] == ch:
            return i

    # If we did not find any occurrence of ch,
    # return -1
    return -1

if __name__ == "__main__":
    s = "geeksforgeeks"
    ch = 'k'
  
    print(findChar(s, ch))
C#
// C# program to search a character in a string

using System;

class GfG {
    
    // function to find the first occurrence of ch in s
    static int findChar(string s, char ch) {
        int n = s.Length;
        for (int i = 0; i < n; i++) {
          
            // If the current character is equal to ch, 
            // return the current index
            if (s[i] == ch)
                return i;
        }

        // If we did not find any occurrence of ch,
        // return -1
        return -1;
    }

    static void Main(string[] args) {
        string s = "geeksforgeeks";
        char ch = 'k';

        Console.WriteLine(findChar(s, ch));
    }
}
JavaScript
// JavaScript program to search a character in a string

// function to find the first occurrence of ch in s
function findChar(s, ch) {
    let n = s.length;
    for (let i = 0; i < n; i++) {
      
        // If the current character is equal to ch, 
        // return the current index
        if (s[i] === ch)
            return i;
    }

    // If we did not find any occurrence of ch,
    // return -1
    return -1;
}

let s = "geeksforgeeks";
let ch = 'k';

console.log(findChar(s, ch));

Output
3

Approach - By Using in-built library functions - O(n) Time and O(1) Space

We can also use inbuilt library functions to search for a character in a string. This makes the search simple and easier to implement.

C++
#include <iostream>
#include <string>

using namespace std;

int findCharacterIndex(const string& s, char ch) {
    size_t idx = s.find(ch);

    if (idx != string::npos) {
        return idx;
    } else {
        return -1;
    }
}

int main() {
    string s = "geeksforgeeks";
    char ch = 'k';

    int index = findCharacterIndex(s, ch);
    cout << index << endl;

    return 0;
}
C
#include <stdio.h>
#include <string.h>

int findCharacterIndex(const char* s, char ch) {
    char* ptr = strchr(s, ch);
    if (ptr != NULL) {
        return ptr - s;
    } else {
        return -1;
    }
}

int main() {
    const char* s = "geeksforgeeks";
    char ch = 'k';

    int index = findCharacterIndex(s, ch);
    printf("%d\n", index);

    return 0;
}
Java
public class GfG{
    public static int findCharacterIndex(String s, char ch) {
        int idx = s.indexOf(ch);
        return (idx != -1) ? idx : -1;
    }

    public static void main(String[] args) {
        String s = "geeksforgeeks";
        char ch = 'k';

        int index = findCharacterIndex(s, ch);
        System.out.println(index);
    }
}
Python
def find_character_index(s, ch):
    idx = s.find(ch)
    return idx 

s = "geeksforgeeks"
ch = 'k'

index = find_character_index(s, ch)
print(index)
C#
using System;

class GfG {
    static int FindCharacterIndex(string s, char ch) {
        int idx = s.IndexOf(ch);
        return idx != -1 ? idx : -1;
    }

    static void Main() {
        string s = "geeksforgeeks";
        char ch = 'k';

        int index = FindCharacterIndex(s, ch);
        Console.WriteLine(index);
    }
}
JavaScript
function findCharacterIndex(s, ch) {
    let idx = s.indexOf(ch);
    return idx !== -1 ? idx : -1;
}

let s = "geeksforgeeks";
let ch = 'k';

let index = findCharacterIndex(s, ch);
console.log(index);

Output
3




Next Article
Article Tags :
Practice Tags :

Similar Reads