strchr in C

Last Updated : 11 Jul, 2026

The strchr() function in C is a predefined string handling function declared in the <string.h> header file. It is used to find the first occurrence of a specified character in a string.

  • Returns a pointer to the first occurrence of the character if it is found.
  • Character is not present in the string, it returns NULL.
C
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "GeeksforGeeks";

    char *ptr = strchr(str, 'f');

    if (ptr != NULL)
        printf("Character found at position: %ld\n", ptr - str);
    else
        printf("Character not found\n");

    return 0;
}

Output
Character found at position: 5

Syntax

char *strchr(const char *str, int ch);

Parameters of strchr() in C

  • str: The string in which the character is searched. It is passed as a constant character pointer and is not modified by the function.
  • ch: The character to search for. It is passed as an int but is internally treated as a character.

Return Value of strchr() 

The strchr() function returns a pointer (char *) to the first occurrence of the specified character in the string. If the character is not found, it returns NULL.

Example Programs Using strchr() in C

The following examples illustrates how we can use strchr() in various scenarios.

Example: Using strchr() to check the existence of character in a string and print its first occurrence.

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

int main()
{
    // define a string
    const char* str = "GeeksforGeeks";
    // define a char ch to be searched in str
    char ch = 's';

    // Use strchr to find the first occurrence of the
    // character 's'
    const char* result = strchr(str, ch);

    if (result != NULL) {
        // Calculate the position by subtracting the base
        // pointer from the result pointer
        printf("Character '%c' found at position: %ld\n",
               ch, result - str);
    }
    else {
        printf("Character '%c' not found.\n", ch);
    }

    return 0;
}

Output
Character 's' found at position: 4

Example: Using strchr() function to parse the string until a given delimiter is found.

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

int main()
{
    // Original string containing username and password
    const char* str = "GeeksforGeeks:abc@123";
    // Delimiter to separate username and password

    char delimiter = ':';
    // Find the position of the delimiter in the string
    char* delimiter_position = strchr(str, delimiter);

    // If the delimiter is found in the string
    if (delimiter_position != NULL) {
        // Calculate the length of the username
        size_t username_length = delimiter_position - str;

        // Allocate memory for the username and copy the
        // username part of the string
        char username[username_length + 1];
        strncpy(username, str, username_length);

        // Null-terminate the username string
        username[username_length] = '\0';

        // The password starts right after the delimiter
        char* password = delimiter_position + 1;

        // Print the extracted username and password
        printf("Username: %s\n", username);
        printf("Password: %s\n", password);
    }
    else {
        // If the delimiter is not found, print an error
        // message
        printf("Delimiter not found.\n");
    }

    return 0;
}

Output
Username: GeeksforGeeks
Password: abc@123


Comment