# 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: Please solve it on PRACTICE first, before moving on to the solution.

Method 1 (Simple : Traverse from left) :
Traverse given string from left to right and keep updating index whenever x matches with current character.

 `// CPP program to find last index of ` `// character x in given string. ` `#include ` `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 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 */`

 `# 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# 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 */`

 ` `

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.

 `// Simple CPP program to find last index of ` `// character x in given string. ` `#include ` `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 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 `

 `# 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# 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 `

 `= 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. ` `?> `

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.

Article Tags :
Practice Tags :