C strstr() Function

The strstr() function in C is a standard library function that locates the first occurrence of a substring in a string. It is part of the C standard library (string.h). This function is useful for finding the position of a substring within a string.

Table of Contents

  1. Introduction
  2. strstr() Function Syntax
  3. Understanding strstr() Function
  4. Examples
    • Locating a Substring in a String
    • Using strstr() with User Input
  5. Real-World Use Case
  6. Conclusion

Introduction

The strstr() function searches for the first occurrence of the substring needle in the string haystack. It returns a pointer to the beginning of the located substring, or NULL if the substring is not found.

strstr() Function Syntax

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

char *strstr(const char *haystack, const char *needle);

Parameters:

  • haystack: A pointer to the null-terminated string to be searched.
  • needle: A pointer to the null-terminated substring to search for.

Returns:

  • The function returns a pointer to the first occurrence of the substring needle in the string haystack. If the substring is not found, the function returns NULL.

Understanding strstr() Function

The strstr() function performs a search for the substring needle within the string haystack. It returns a pointer to the first character of the first occurrence of needle in haystack. If needle is an empty string, the function returns haystack.

Examples

Locating a Substring in a String

To demonstrate how to use strstr() to locate a substring in a string, we will write a simple program.

Example

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

int main() {
    char haystack[] = "Hello, World!";
    char needle[] = "World";

    // Locate the substring using strstr
    char *result = strstr(haystack, needle);

    // Print the result
    if (result != NULL) {
        printf("Found substring '%s' at position %ld\n", needle, result - haystack);
    } else {
        printf("Substring not found\n");
    }

    return 0;
}

Output:

Found substring 'World' at position 7

Using strstr() with User Input

This example shows how to use strstr() to locate a substring in a user-provided string.

Example

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

int main() {
    char haystack[100];
    char needle[50];

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

    // Get user input for needle
    printf("Enter the substring to find: ");
    fgets(needle, sizeof(needle), stdin);
    needle[strcspn(needle, "\n")] = '\0';  // Remove newline character

    // Locate the substring using strstr
    char *result = strstr(haystack, needle);

    // Print the result
    if (result != NULL) {
        printf("Found substring '%s' at position %ld\n", needle, result - haystack);
    } else {
        printf("Substring not found\n");
    }

    return 0;
}

Output (example user input "Hello, Ramesh!" and substring "Ramesh"):

Enter the main string: Hello, Ramesh!
Enter the substring to find: Ramesh
Found substring 'Ramesh' at position 7

Real-World Use Case

Parsing Configuration Files

In real-world applications, the strstr() function can be used to parse configuration files and locate specific keys or settings.

Example: Parsing a Configuration File

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

int main() {
    char config[] = "username=admin\npassword=secret\nhost=localhost\n";
    char key[] = "password=";

    // Locate the key using strstr
    char *result = strstr(config, key);

    // Print the result
    if (result != NULL) {
        // Move the pointer to the value part
        result += strlen(key);
        printf("Found value for '%s': %s\n", key, result);
    } else {
        printf("Key not found\n");
    }

    return 0;
}

Output:

Found value for 'password=': secret
host=localhost

Conclusion

The strstr() function is used for locating the first occurrence of a substring in a string in C. By understanding and using this function correctly, you can efficiently search for substrings 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