Method 1 (Simple : Traverse from left) :
Traverse given string from left to right and keep updating index whenever x matches with current character.
C++
filter_none
edit close
play_arrow
link brightness_4 code
// 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;
}
chevron_right
filter_none
Java
filter_none
edit close
play_arrow
link brightness_4 code
// 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 */
chevron_right
filter_none
Python3
filter_none
edit close
play_arrow
link brightness_4 code
# 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.
chevron_right
filter_none
C#
filter_none
edit close
play_arrow
link brightness_4 code
// 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 */
chevron_right
filter_none
PHP
filter_none
edit close
play_arrow
link brightness_4 code
<?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.
?>
chevron_right
filter_none
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
filter_none
edit close
play_arrow
link brightness_4 code
// 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;
}
chevron_right
filter_none
Java
filter_none
edit close
play_arrow
link brightness_4 code
// 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
chevron_right
filter_none
Python3
filter_none
edit close
play_arrow
link brightness_4 code
# 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
chevron_right
filter_none
C#
filter_none
edit close
play_arrow
link brightness_4 code
// 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
chevron_right
filter_none
PHP
filter_none
edit close
play_arrow
link brightness_4 code
<?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.
?>
chevron_right
filter_none
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.
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