C Program to Extract a Portion of String

By | January 6, 2022
C Program to Extract a Portion of String

Extracting a substring from a string is one of the most common operations that you may need to use in your code. In C, it is quite easy to extract a portion of a string with the intention of using it separately from the parent string.

In this tutorial, we have written a C program that can extract a substring from a given string. However, before sharing the C program, let’s take a look at the steps that the program follows to extract a substring from the string input by a user.

Steps to Create the Program

  • Ask the user to enter a string.
  • Ask the user to enter the starting and endpoint of the string they want to extract. These points will represent the index value of the String.
  • Pass the string, starting point, and ending point as the arguments to the user-defined function sub_string().
  • The sub_string() function prints a sequence of characters that represents the substring that the user has requested.

C Program to Extract a Portion of a String

#include <conio.h>
void sub_string(char *s, int start,int end)
       int i;
       for(i=start; i<= end; i++)
void main()
      char str[100];
      int s,e;

      printf("Enter a String: ");

      printf("Enter the starting Index: ");

      printf("Enter the Last Index: ");

      if(e > strlen(str) || (s>strlen(str)))
            printf("Starting or End value of Index is out of Range");




Enter a String: Welcome To TechGeekBuzz
Enter the starting Index: 1
Enter the Last Index: 9
elcome To


gets(): It is an in-built C function that can take string input from the user, and it is similar to the scanf() function.

To Sum it Up

Extracting a substring from a string in C is quite simple. In our program above, we have written a function that accepts three arguments, namely the input string, the starting index number, and the ending index number, and outputs a substring. Also, the C program that extracts a portion of string prompts a user to input the string along with the starting and ending index number to extract the substring.

People are also Reading

Author: Vinay Singh

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published.