What are string searching functions in C language?

String searching functions in C are built-in library functions that help locate specific characters, substrings, or patterns within strings. These functions are part of the standard C library (string.h) and provide efficient ways to search and analyze string content.

Syntax

#include <string.h>

char *strchr(const char *str, int c);
char *strrchr(const char *str, int c);
char *strpbrk(const char *s1, const char *s2);
size_t strspn(const char *s1, const char *s2);
size_t strcspn(const char *s1, const char *s2);
char *strtok(char *s1, const char *s2);
char *strtok_r(char *s1, const char *s2, char **saveptr);

String Searching Functions Overview

The following table lists the primary string searching functions available in C −

Function Description
strchr() Find the first occurrence of character 'c' in the string
strrchr() Find the last occurrence of character 'c' in the string
strpbrk() Returns a pointer to the first occurrence of any character from string s2 in string s1
strspn() Returns the number of characters at the beginning of s1 that match s2
strcspn() Returns the number of characters at the beginning of s1 that do not match s2
strtok() Break the string into a sequence of tokens using delimiters
strtok_r() Thread-safe version of strtok() that requires a saveptr parameter

Example 1: Using strchr() Function

The strchr() function finds the first occurrence of a character in a string −

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

int main() {
    char *str1 = "Hello World";
    char *result;
    
    result = strchr(str1, 'l');
    if (result != NULL) {
        printf("First 'l' found at: %s
", result); printf("Position: %ld
", result - str1); } else { printf("Character not found
"); } return 0; }
First 'l' found at: llo World
Position: 2

Example 2: Using strpbrk() Function

The strpbrk() function finds the first occurrence of any character from a set −

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

int main() {
    char *str1 = "Hello World";
    char *result;
    
    result = strpbrk(str1, "aeiou");
    if (result != NULL) {
        printf("First vowel found: %c
", *result); printf("Remaining string: %s
", result); } else { printf("No vowels found
"); } return 0; }
First vowel found: e
Remaining string: ello World

Example 3: Using strtok() for String Tokenization

The strtok() function splits a string into tokens based on delimiters −

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

int main() {
    char str[] = "apple,banana,cherry";
    char *token;
    
    printf("Original string: %s
", str); printf("Tokens:
"); token = strtok(str, ","); while (token != NULL) { printf("- %s
", token); token = strtok(NULL, ","); } return 0; }
Original string: apple,banana,cherry
Tokens:
- apple
- banana
- cherry

Key Points

  • All string searching functions require #include <string.h>
  • strchr() and strrchr() return pointers to the found character or NULL
  • strspn() and strcspn() return the count of matching/non-matching characters
  • strtok() modifies the original string by inserting null terminators
  • strtok_r() is the thread-safe version of strtok()

Conclusion

String searching functions in C provide powerful tools for locating characters, patterns, and splitting strings. These functions are essential for text processing and string manipulation tasks in C programming.

Updated on: 2026-03-15T14:18:19+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements