Find last index of a character in a string
Last Updated :
27 Jul, 2022
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
Method 1 (Simple : Traverse from left): Traverse given string from left to right and keep updating index whenever x matches with current character.
Implementation:
C++
// CPP program to find last index of
// character x in given string.
#include <iostream>
using namespace std;
// Returns last index of x if it is present.
// Else returns -1.
int findLastIndex(string& str, char x)
{
int index = -1;
for (int i = 0; i < str.length(); i++)
if (str[i] == x)
index = i;
return index;
}
// Driver code
int main()
{
// String in which char is to be found
string str = "geeksforgeeks";
// char whose index is to be found
char x = 'e';
int index = findLastIndex(str, x);
if (index == -1)
cout << "Character not found";
else
cout << "Last index is " << index;
return 0;
}
Java
// Java program to find last index
// of character x in given string.
import java.io.*;
class GFG {
// Returns last index of x if
// it is present Else returns -1.
static int findLastIndex(String str, Character x)
{
int index = -1;
for (int i = 0; i < str.length(); i++)
if (str.charAt(i) == x)
index = i;
return index;
}
// Driver code
public static void main(String[] args)
{
// String in which char is to be found
String str = "geeksforgeeks";
// char whose index is to be found
Character x = 'e';
int index = findLastIndex(str, x);
if (index == -1)
System.out.println("Character not found");
else
System.out.println("Last index is " + index);
}
}
/* This code is contributed by Prerna Saini */
Python3
# A Python program to find last
# index of character x in given
# string.
# Returns last index of x if it
# is present. Else returns -1.
def findLastIndex(str, x):
index = -1
for i in range(0, len(str)):
if str[i] == x:
index = i
return index
# Driver program
# String in which char is to be found
str = "geeksforgeeks"
# char whose index is to be found
x = 'e'
index = findLastIndex(str, x)
if index == -1:
print("Character not found")
else:
print('Last index is', index)
# This code is contributed by shrikant13.
C#
// C# program to find last index
// of character x in given string.
using System;
class GFG {
// Returns last index of x if
// it is present Else returns -1.
static int findLastIndex(string str, char x)
{
int index = -1;
for (int i = 0; i < str.Length; i++)
if (str[i] == x)
index = i;
return index;
}
// Driver code
public static void Main()
{
// String in which char is to be found
string str = "geeksforgeeks";
// char whose index is to be found
char x = 'e';
int index = findLastIndex(str, x);
if (index == -1)
Console.WriteLine("Character not found");
else
Console.WriteLine("Last index is " + index);
}
}
/* This code is contributed by vt_m */
PHP
<?php
// PHP program to find last index of
// character x in given string.
// Returns last index of
// x if it is present.
// Else returns -1.
function findLastIndex($str, $x)
{
$index = -1;
for ($i = 0; $i < strlen($str); $i++)
if ($str[$i] == $x)
$index = $i;
return $index;
}
// Driver code
// String in which
// char is to be found
$str = "geeksforgeeks";
// char whose index
// is to be found
$x = 'e';
$index = findLastIndex($str, $x);
if ($index == -1)
echo("Character not found");
else
echo("Last index is " . $index);
// This code is contributed by Ajit.
?>
JavaScript
<script>
// javascript program to find last index
// of character x in given string.
// Returns last index of x if
// it is present Else returns -1.
function findLastIndex(str, x)
{
let index = -1;
for (let i = 0; i < str.length; i++)
if (str[i] == x)
index = i;
return index;
}
// Driver code
// String in which char is to be found
let str = "geeksforgeeks";
// char whose index is to be found
let x = 'e';
let index = findLastIndex(str, x);
if (index == -1)
document.write("Character not found");
else
document.write("Last index is " + index);
// This code is contributed by sanjoy_62.
</script>
Time Complexity : O(n)
Auxiliary Space: O(1)
Method 2 (Efficient : Traverse from right): In above method 1, we always traverse complete string. In this method, we can avoid complete traversal in all those cases when x is present. The idea is to traverse from right side and stop as soon as we find character.
Implementation:
CPP
// Simple CPP program to find last index of
// character x in given string.
#include <iostream>
using namespace std;
// Returns last index of x if it is present.
// Else returns -1.
int findLastIndex(string& str, char x)
{
// Traverse from right
for (int i = str.length() - 1; i >= 0; i--)
if (str[i] == x)
return i;
return -1;
}
// Driver code
int main()
{
string str = "geeksforgeeks";
char x = 'e';
int index = findLastIndex(str, x);
if (index == -1)
cout << "Character not found";
else
cout << "Last index is " << index;
return 0;
}
Java
// Java code to find last index
// character x in given string.
import java.io.*;
class GFG {
// Returns last index of x if
// it is present. Else returns -1.
static int findLastIndex(String str, Character x)
{
// Traverse from right
for (int i = str.length() - 1; i >= 0; i--)
if (str.charAt(i) == x)
return i;
return -1;
}
// Driver code
public static void main(String[] args)
{
String str = "geeksforgeeks";
Character x = 'e';
int index = findLastIndex(str, x);
if (index == -1)
System.out.println("Character not found");
else
System.out.println("Last index is " + index);
}
}
// This code is contributed by Prerna Saini
Python3
# Simple Python3 program to find last
# index of character x in given string.
# Returns last index of x if it is
# present. Else returns -1.
def findLastIndex(str, x):
# Traverse from right
for i in range(len(str) - 1, -1,-1):
if (str[i] == x):
return i
return -1
# Driver code
str = "geeksforgeeks"
x = 'e'
index = findLastIndex(str, x)
if (index == -1):
print("Character not found")
else:
print("Last index is " ,index)
# This code is contributed by Smitha
C#
// C# code to find last index
// character x in given string.
using System;
class GFG {
// Returns last index of x if
// it is present. Else returns -1.
static int findLastIndex(string str, char x)
{
// Traverse from right
for (int i = str.Length - 1; i >= 0; i--)
if (str[i] == x)
return i;
return -1;
}
// Driver code
public static void Main()
{
string str = "geeksforgeeks";
char x = 'e';
int index = findLastIndex(str, x);
if (index == -1)
Console.WriteLine("Character not found");
else
Console.WriteLine("Last index is " + index);
}
}
// This code is contributed by vt_m
PHP
<?php
// Simple PHP program to find last index
// of character x in given string.
// Returns last index of x if it
// is present. Else returns -1.
function findLastIndex($str, $x)
{
// Traverse from right
for ($i = strlen($str) - 1; $i >= 0; $i--)
if ($str[$i] == $x)
return $i;
return -1;
}
// Driver code
$str = "geeksforgeeks";
$x = 'e';
$index = findLastIndex($str, $x);
if ($index == -1)
echo("Character not found");
else
echo("Last index is " . $index);
// This code is contributed by Ajit.
?>
JavaScript
<script>
// Javascript code to find last index character x in given string.
// Returns last index of x if
// it is present. Else returns -1.
function findLastIndex(str, x)
{
// Traverse from right
for (let i = str.length - 1; i >= 0; i--)
if (str[i] == x)
return i;
return -1;
}
let str = "geeksforgeeks";
let x = 'e';
let index = findLastIndex(str, x);
if (index == -1)
document.write("Character not found");
else
document.write("Last index is " + index);
</script>
Time Complexity : O(n)
Auxiliary Space: O(1)
Similar Reads
Find Minimum Indexed Character in String Given a string str and another string patt. The task is to find the character in patt that is present at the minimum index in str. If no character of patt is present in str then print "$".Examples: Input: str = "geeksforgeeks", patt = "set" Output: e Both e and s of patt are present in str, but e is
14 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
Accessing characters by index in a String Given two strings str1 and an index, the task is to access the character at this index. Examples: Input: str = "hello", index = 1Output: e Input: str = "GfG", index = 2Output: G Accessing characters by index in a string:Individual characters in a string can be accessed by specifying the string name
2 min read
Find repeated character present first in a string Given a string, find the repeated character present first in the string.(Not the first repeated character, found here.) Examples: Input : geeksforgeeks Output : g (mind that it will be g, not e.) Asked in: Goldman Sachs internship Simple Solution using O(N^2) complexity: The solution is to loop thro
15 min read
Find the Nth occurrence of a character in the given String Given string str, a character ch, and a value N, the task is to find the index of the Nth occurrence of the given character in the given string. Print -1 if no such occurrence exists. Examples: Input: str = "Geeks", ch = 'e', N = 2 Output: 2 Input: str = "GFG", ch = 'e', N = 2 Output: -1 Recommended
7 min read
Find i'th Index character in a binary string obtained after n iterations Given a decimal number m, convert it into a binary string and apply n iterations. In each iteration, 0 becomes "01" and 1 becomes "10". Find the (based on indexing) index character in the string after the nth iteration. Examples: Input : m = 5, n = 2, i = 3Output : 1Input : m = 3, n = 3, i = 6Output
6 min read
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
Program to Search a Character in a String 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: 3Explanation: The character 'k' is present at index 3 and 11 in "g
6 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 iâth index character in a binary string obtained after n iterations | Set 2 Given a decimal number m, convert it into a binary string and apply n iterations, in each iteration 0 becomes â01â and 1 becomes â10â. Find ith(based indexing) index character in the string after nth iteration.Examples: Input: m = 5 i = 5 n = 3Output: 1ExplanationIn the first case m = 5, i = 5, n =
12 min read