Method 1 (Simple : Traverse from left) : Traverse given string from left to right and keep updating index whenever x matches with current character.
C++
// CPP program to find last index of
// character x in given string.
#include <iostream>
usingnamespacestd;
// Returns last index of x if it is present.
// Else returns -1.
intfindLastIndex(string& str, charx)
{
intindex = -1;
for(inti = 0; i < str.length(); i++)
if(str[i] == x)
index = i;
returnindex;
}
// Driver code
intmain()
{
// String in which char is to be found
string str = "geeksforgeeks";
// char whose index is to be found
charx = 'e';
intindex = findLastIndex(str, x);
if(index == -1)
cout << "Character not found";
else
cout << "Last index is "<< index;
return0;
}
Java
// Java program to find last index
// of character x in given string.
importjava.io.*;
classGFG {
// Returns last index of x if
// it is present Else returns -1.
staticintfindLastIndex(String str, Character x)
{
intindex = -1;
for(inti = 0; i < str.length(); i++)
if(str.charAt(i) == x)
index = i;
returnindex;
}
// Driver code
publicstaticvoidmain(String[] args)
{
// String in which char is to be found
String str = "geeksforgeeks";
// char whose index is to be found
Character x = 'e';
intindex = 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.
deffindLastIndex(str, x):
index =-1
fori inrange(0, len(str)):
ifstr[i] ==x:
index =i
returnindex
# 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)
ifindex ==-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.
usingSystem;
classGFG {
// Returns last index of x if
// it is present Else returns -1.
staticintfindLastIndex(stringstr, charx)
{
intindex = -1;
for(inti = 0; i < str.Length; i++)
if(str[i] == x)
index = i;
returnindex;
}
// Driver code
publicstaticvoidMain()
{
// String in which char is to be found
stringstr = "geeksforgeeks";
// char whose index is to be found
charx = 'e';
intindex = 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.
functionfindLastIndex($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.
functionfindLastIndex(str, x)
{
let index = -1;
for(let i = 0; i < str.length; i++)
if(str[i] == x)
index = i;
returnindex;
}
// 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>
Output:
Last index is 10
Time Complexity : Θ(n) 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.
CPP
// Simple CPP program to find last index of
// character x in given string.
#include <iostream>
usingnamespacestd;
// Returns last index of x if it is present.
// Else returns -1.
intfindLastIndex(string& str, charx)
{
// Traverse from right
for(inti = str.length() - 1; i >= 0; i--)
if(str[i] == x)
returni;
return-1;
}
// Driver code
intmain()
{
string str = "geeksforgeeks";
charx = 'e';
intindex = findLastIndex(str, x);
if(index == -1)
cout << "Character not found";
else
cout << "Last index is "<< index;
return0;
}
Java
// Java code to find last index
// character x in given string.
importjava.io.*;
classGFG {
// Returns last index of x if
// it is present. Else returns -1.
staticintfindLastIndex(String str, Character x)
{
// Traverse from right
for(inti = str.length() - 1; i >= 0; i--)
if(str.charAt(i) == x)
returni;
return-1;
}
// Driver code
publicstaticvoidmain(String[] args)
{
String str = "geeksforgeeks";
Character x = 'e';
intindex = 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.
deffindLastIndex(str, x):
# Traverse from right
fori inrange(len(str) -1, -1,-1):
if(str[i] ==x):
returni
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.
usingSystem;
classGFG {
// Returns last index of x if
// it is present. Else returns -1.
staticintfindLastIndex(stringstr, charx)
{
// Traverse from right
for(inti = str.Length - 1; i >= 0; i--)
if(str[i] == x)
returni;
return-1;
}
// Driver code
publicstaticvoidMain()
{
stringstr = "geeksforgeeks";
charx = 'e';
intindex = 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.
functionfindLastIndex($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.
functionfindLastIndex(str, x)
{
// Traverse from right
for(let i = str.length - 1; i >= 0; i--)
if(str[i] == x)
returni;
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>
Output:
Last index is 10
Time Complexity : O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy