Last index of a character

Last Updated : 17 May, 2026

Given a string s and a character x, the task is to find the last index (0 based indexing) of x in string s. If no index found then the answer will be -1.

Examples : 

Input: s = "Geeks", x = 'e'
Output: 2
Explanation: Last index of 'e' is 2.

Input: s = "okiyh", x = 'z'
Output: -1
Explanation: There is no character as 'z'.

Try It Yourself
redirect icon

Using Traversal from Left - O(n) Time O(1) Space

The Idea is to traverse the string once and update the index whenever the given character is found. The last updated value will be the final answer. If the character is not found, return -1.

C++
#include <iostream>
using namespace std;

// Returns last index of x if it is present.
// Else returns -1.
int lastIndex(string &s, char p)
{

    int index = -1;
    
    // Traverse from left
    for (int i = 0; i < s.length(); i++)
        if (s[i] == p)
            index = i;

    return index;
}

// Driver code
int main()
{
    string s = "Geeks";
    
    char x = 'e';

    int index = lastIndex(s, x);
    cout << index;

    return 0;
}
C
#include <stdio.h>
#include <string.h>

// Returns last index of x if it is present.
// Else returns -1.
int lastIndex(char *s, char p)
{
    int index = -1;
    int len = strlen(s);

    // Traverse from left
    for (int i = 0; i < len; i++)
        if (s[i] == p)
            index = i;

    return index;
}

// Driver code
int main()
{
    char s[] = "Geeks";

    char x = 'e';

    int index = lastIndex(s, x);
    printf("%d", index);

    return 0;
}
Java
public class GfG {
    // Returns last index of x if it is present.
    // Else returns -1.
    public static int lastIndex(String s, char p) {
        int index = -1;

        // Traverse from left
        for (int i = 0; i < s.length(); i++)
            if (s.charAt(i) == p)
                index = i;

        return index;
    }

    public static void main(String[] args) {
        String s = "Geeks";

        char x = 'e';

        int index = lastIndex(s, x);
        System.out.println(index);
    }
}
Python
def lastIndex(s, p):
    
    index = -1
    
    # Traverse from left
    for i in range(len(s)):
        if s[i] == p:
            index = i

    return index

# Driver code
if __name__ == "__main__":

    s = "Geeks"
    x = 'e'

    index = lastIndex(s, x)
    print(index)
C#
using System;

public class GfG
{
    // Returns last index of x if it is present.
    // Else returns -1.
    public static int lastIndex(string s, char p)
    {
        int index = -1;
        
        // Traverse from left
        for (int i = 0; i < s.Length; i++)
            if (s[i] == p)
                index = i;
        
        return index;
    }
    
    public static void Main()
    {
        string s = "Geeks";
        
        char x = 'e';
        
        int index = lastIndex(s, x);
        Console.WriteLine(index);
    }
}
JavaScript
// Returns last index of x if it is present.
// Else returns -1.
function lastIndex(s, p) {
    let index = -1;
    
    // Traverse from left
    for (let i = 0; i < s.length; i++) {
        if (s[i] === p) {
            index = i;
        }
    }
    
    return index;
}

// Driver code
const s = "Geeks";
const x = 'e';
const index = lastIndex(s, x);
console.log(index);

Output
2

Time Complexity : O(n)
Auxiliary Space: O(1)

Using Traversal from Right - O(n) Time and O(1) Space

The idea is to traverse the given string from right to left. Since we move from the end, the first match we encounter will be the last occurrence of that character in the string. If we find the character, we return its index immediately.

C++
#include <iostream>
using namespace std;

// Returns last index of x if it is present.
// Else returns -1.
int lastIndex(string &s, char p)
{

    // Traverse from right
    for (int i = s.length() - 1; i >= 0; i--)
        if (s[i] == p)
            return i;

    return -1;
}

// Driver code

int main()
{
    string s = "Geeks";
    char x = 'e';

    int index = lastIndex(s, x);

    cout << index;

    return 0;
}
C
#include <stdio.h>
#include <string.h>

// Returns last index of x if it is present.
// Else returns -1.
int lastIndex(char *s, char p)
{
    int length = strlen(s);

    // Traverse from right
    for (int i = length - 1; i >= 0; i--)
        if (s[i] == p)
            return i;

    return -1;
}

// Driver code
int main()
{
    char s[] = "Geeks";
    char x = 'e';

    int index = lastIndex(s, x);

    printf("%d", index);

    return 0;
}
Java
public class GfG {

    // Returns last index of x if it is present.
    // Else returns -1.
    public static int lastIndex(String s, char p) {

        // Traverse from right
        for (int i = s.length() - 1; i >= 0; i--)
            if (s.charAt(i) == p)
                return i;

        return -1;
    }

    // Driver code
    public static void main(String[] args) {
        String s = "Geeks";
        char x = 'e';

        int index = lastIndex(s, x);

        System.out.println(index);
    }
}
Python
def lastIndex(s, p):

    # Traverse from right
    for i in range(len(s) - 1, -1, -1):
        if s[i] == p:
            return i

    return -1

# Driver code
if __name__ == "__main__":

    s = "Geeks"
    x = 'e'

    index = lastIndex(s, x)
    print(index)
C#
using System;

public class GfG {

    // Returns last index of x if it is present.
    // Else returns -1.
    public static int lastIndex(string s, char p) {

        // Traverse from right
        for (int i = s.Length - 1; i >= 0; i--)
            if (s[i] == p)
                return i;

        return -1;
    }

    // Driver code
    public static void Main() {
        string s = "Geeks";
        char x = 'e';

        int index = lastIndex(s, x);

        Console.WriteLine(index);
    }
}
JavaScript
// Returns last index of x if it is present.
// Else returns -1.
function lastIndex(s, p) {

    // Traverse from right
    for (let i = s.length - 1; i >= 0; i--)
        if (s[i] === p)
            return i;

    return -1;
}

// Driver code
let s = "Geeks";
let x = 'e';

let index = lastIndex(s, x);

console.log(index);

Output
2

Time Complexity : O(n) 
Auxiliary Space: O(1)

Comment