C Program to Fetch a Substring From a String

Here is a C program to fetch substring from a given string. Given a string, left_Index and length of sub-string(length) as input from user, we have to return a sub-string of input string containing characters from left_Index to left_Index + length.

For example
Input String: TechCrashCourse
left index = 2
Length of substring = 7
Then, Substring = chCrash


Points to Remember
  • Length of sub-string should be less than or equal to input String.
  • There is no function inside string.h header file which directly find sub-string.
  • Destination and source should not overlap.

We can either use srtncpy function of string.h to find substring or we can use user defined substring function using pointers.


C program to fetch substring of a string using strncpy function

Here is the declaration for strncpy() function
char *strncpy (char *destination, const char *source, size_t num);

In this program we are using an extra subString character array to store sub-string form input array. We initialize it with null character using memset function.

Function strncpy copies the first num characters from source to destination string. In case where the length of source is less than num, the remainder of destination will is padded with zeros until a total of num characters have been written to it.

We can use strncpy function to get substring, if we pass source string pointer as source + starting_index, which is same as starting position of substring in inputstring.

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

int main(){
    char inputString[100], subString[100];
    int index, subStringLength;
    memset(subString, '\0', sizeof(subString));
    
    printf("Enter a String \n");
    gets(inputString);
    
    printf("Enter starting position of sub-string and it's length \n");
    scanf("%d %d", &index, &subStringLength);
    
    strncpy(subString, inputString + index, subStringLength);
    
    printf("SubString is : %s \n", subString);
    return 0;
}
Output
Enter a String 
TechCrashCourse
Enter starting position of sub-string and it's length 
3 7
SubString is : hCrashC

C program to print sub-string of a string using function and pointers

In this program, we first takes a string as input from user using gets function. Here we are using a user-defined function getSubString, which takes source string pointer, destination string pointer, starting index and substring length(num) as input arguments and returns substring. We copy num characters from source string starting from index till index + num. At last we add a null character after the last character or substring.

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

char* getSubString(char* inputStringLength, char* subString,
    int index, int subStringLength);
    
int main(){
    char inputString[100], subString[100];
    int index, subStringLength;
    
    printf("Enter a String \n");
    gets(inputString);
    
    printf("Enter starting position of sub-string and it's length \n");
    scanf("%d %d", &index, &subStringLength);

    printf("SubString is : %s \n", getSubString(inputString,
        subString, index, subStringLength));

    return 0;
}

char* getSubString(char* inputString, char* subString,
    int index, int subStringLength){
    int counter, inputStringLength = strlen(inputString);    

    if(index < 0 || index > inputStringLength || 
          (index + subStringLength) > inputStringLength){
        printf("Invalid Input");
        return NULL;
    }
    for(counter = 0; counter < subStringLength; counter++){
        subString[counter] = inputString[index++];
    }
    subString[counter] = '\0';
    
    return subString;
}
Output
Enter a String 
C Programming is awesome
Enter starting position of sub-string and it's length 
2 10
SubString is : Programmin

Related Topics
C program to check if two strings are anagram
C Program to find frequency of characters in a string
C program to convert lowercase string to uppercase
C program to convert uppercase string to lowercase
C program to remove vowels from a string
C program to find length of string
C program to reverse a string
List of all C programs