C Find Substring Program


The "Find Substring" program in C is designed to search for a specific substring within a larger string and determine its position (index) within that string. This is a common task in string manipulation and involves comparing portions of the larger string with the substring.

Implementation of Find Substring Program

Here’s a simple implementation of this program in C:

#include <stdio.h> #include <string.h> int main() { char str[100], sub[50]; // Arrays to hold the main string and the substring char *pos; // Pointer to hold the position of the substring int index; // Variable to store the index of the found substring // Ask user for input printf("Enter the main string: "); fgets(str, sizeof(str), stdin); // Read the main string str[strcspn(str, "\n")] = '\0'; // Remove newline character printf("Enter the substring to find: "); fgets(sub, sizeof(sub), stdin); // Read the substring sub[strcspn(sub, "\n")] = '\0'; // Remove newline character // Find the substring pos = strstr(str, sub); // Use strstr() to find the first occurrence if (pos != NULL) { index = pos - str; // Calculate the index of the found substring printf("Substring found at index: %d\n", index); } else { printf("Substring not found.\n"); } return 0; }

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: For standard input and output functions.
    • #include <string.h>: For string manipulation functions, especially strstr().
  2. Variable Declarations:

    • char str[100]: An array to hold the main string (up to 99 characters).
    • char sub[50]: An array to hold the substring (up to 49 characters).
    • char *pos: A pointer that will point to the location of the substring in the main string.
    • int index: An integer to store the index of the found substring.
  3. User Input:

    • The program prompts the user to enter the main string and the substring. It uses fgets() to read both strings, allowing for spaces.
  4. Remove Newline Character:

    • The newline character introduced by fgets() is removed using strcspn().
  5. Finding the Substring:

    • The strstr() function is used to find the first occurrence of the substring within the main string. It returns a pointer to the start of the substring if found; otherwise, it returns NULL.
  6. Check for Substring:

    • An if statement checks whether pos is not NULL. If it is not NULL, it means the substring was found.
  7. Calculate Index:

    • The index of the substring is calculated by subtracting the address of the main string from the address of the substring pointer (pos).
  8. Display Results:

    • The program prints the index of the first occurrence of the substring if found; otherwise, it indicates that the substring was not found.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code.

    gcc find_substring.c -o find_substring
  2. Execute the Program:

    ./find_substring
  3. Input Data: Enter the main string and the substring when prompted.

Example Input/Output

Input:

Enter the main string: Hello, welcome to the world of programming. Enter the substring to find: welcome

Output:

Substring found at index: 7

Conclusion

The "Find Substring" program in C demonstrates how to search for a substring within a string using pointers and the standard library function strstr(). This program is fundamental in understanding string manipulation, memory addressing, and how to work with character arrays in C. It can be extended to find all occurrences of the substring or to perform case-insensitive searches with additional logic.