C strrchr() Function

The strrchr() function in C is a standard library function that locates the last occurrence of a specified character in a string. It is part of the C standard library (string.h). This function is useful for finding the position of the last occurrence of a character within a string.

Table of Contents

  1. Introduction
  2. strrchr() Function Syntax
  3. Understanding strrchr() Function
  4. Examples
    • Locating the Last Occurrence of a Character in a String
    • Using strrchr() with User Input
  5. Real-World Use Case
  6. Conclusion

Introduction

The strrchr() function searches for the last occurrence of a specified character in a null-terminated string. It returns a pointer to the last occurrence of the character, or NULL if the character is not found.

strrchr() Function Syntax

The syntax for the strrchr() function is as follows:

char *strrchr(const char *str, int c);

Parameters:

  • str: A pointer to the null-terminated string to be scanned.
  • c: The character to be located, passed as an int, but it is internally converted to a char.

Returns:

  • The function returns a pointer to the last occurrence of the character c in the string str. If the character is not found, the function returns NULL.

Understanding strrchr() Function

The strrchr() function performs a reverse search for the specified character in the given string. It stops searching as soon as it finds the character or reaches the beginning of the string. The search includes the null terminator (\0), so if c is \0, the function will return a pointer to the null terminator of the string.

Examples

Locating the Last Occurrence of a Character in a String

To demonstrate how to use strrchr() to locate the last occurrence of a character in a string, we will write a simple program.

Example

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello, World!";
    char target = 'o';

    // Locate the last occurrence of the character using strrchr
    char *result = strrchr(str, target);

    // Print the result
    if (result != NULL) {
        printf("Found '%c' at position %ld\n", target, result - str);
    } else {
        printf("Character not found\n");
    }

    return 0;
}

Output:

Found 'o' at position 8

Using strrchr() with User Input

This example shows how to use strrchr() to locate the last occurrence of a character in a user-provided string.

Example

#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    char target;

    // Get user input for str
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = '\0';  // Remove newline character

    // Get the target character
    printf("Enter a character to find: ");
    target = getchar();

    // Locate the last occurrence of the character using strrchr
    char *result = strrchr(str, target);

    // Print the result
    if (result != NULL) {
        printf("Found '%c' at position %ld\n", target, result - str);
    } else {
        printf("Character not found\n");
    }

    return 0;
}

Output (example user input "Hello, Ramesh!" and target 'e'):

Enter a string: Hello, Ramesh!
Enter a character to find: e
Found 'e' at position 10

Real-World Use Case

Extracting File Extension

In real-world applications, the strrchr() function can be used to extract the file extension from a file path by locating the last occurrence of the dot (.) character.

Example: Extracting File Extension

#include <stdio.h>
#include <string.h>

int main() {
    char file_path[] = "/home/user/documents/report.txt";

    // Locate the last occurrence of the dot character using strrchr
    char *ext = strrchr(file_path, '.');

    // Print the file extension
    if (ext != NULL) {
        printf("File extension: %s\n", ext);
    } else {
        printf("No file extension found\n");
    }

    return 0;
}

Output:

File extension: .txt

Conclusion

The strrchr() function is used for locating the last occurrence of a character in a string in C. By understanding and using this function correctly, you can efficiently search for characters within strings and handle various parsing tasks in your programs. This is particularly helpful in applications that involve string manipulation, searching, and parsing operations.

Comments