Program to Search a Character in a String
Last Updated :
25 Mar, 2025
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));
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);
Similar Reads
Searching For Characters and Substring in a String in Java Efficient String manipulation is very important in Java programming especially when working with text-based data. In this article, we will explore essential methods like indexOf(), contains(), and startsWith() to search characters and substrings within strings in Java.Searching for a Character in a
5 min read
Print the middle character of a string Given string str, the task is to print the middle character of a string. If the length of the string is even, then there would be two middle characters, we need to print the second middle character. Examples: Input: str = "Java"Output: vExplanation: The length of the given string is even. Therefore,
3 min read
Find last index of a character in a string Given a string str and a character x, find last index of x in str. Examples : Input : str = "geeks", x = 'e' Output : 2 Last index of 'e' in "geeks" is: 2 Input : str = "Hello world!", x = 'o' Output : 7 Last index of 'o' is: 7 Recommended PracticeLast index of a character in the stringTry It! Metho
8 min read
Program to accept String starting with Capital letter Given a string str consisting of alphabets, the task is to check whether the given string is starting with a Capital Letter or Not.Examples: Input: str = "GeeksforGeeks"Output: Accepted Input: str = "geeksforgeeks"Output: Not Accepted Approach: Find the ASCII value of the first character of the stri
3 min read
Count Occurrences of a Given Character in a String Given a string S and a character 'c', the task is to count the occurrence of the given character in the string.Examples: Input : S = "geeksforgeeks" and c = 'e'Output : 4Explanation: 'e' appears four times in str.Input : S = "abccdefgaa" and c = 'a' Output : 3Explanation: 'a' appears three times in
6 min read