Get substring c – C Program to Fetch a Substring From a String

  • Write a program in c to fetch substring in a given string.
  • How to extract a substring from 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

Get substring c: 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.

C Program to Fetch a Substring From a String

/*
* C Program to print sub-string of a string
*/
#include <stdio.h>
#include <conio.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);
    getch();
    return 0;
}

Program 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

Get substring in c: 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.

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

/*
* C Program to print sub-string of a string
*/
#include <stdio.h>
#include <conio.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));
    getch();
    return 0;
}
 
/*
 * Function to return substring of inputString starting
 * at position index and of length subStringLength
 */
char* getSubString(char* inputString, char* subString,
    int index, int subStringLength){
    int counter, inputStringLength = strlen(inputString);   
    /* Input validation
     * range of sub-string must be in [0, 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;
}

Program Output

Enter a String 
C Programming is awesome
Enter starting position of sub-string and it's length 
2 10
SubString is : Programmin