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
Output:
Note: 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 a string prompts a user to input the string along with the starting and ending index number to extract the substring.
People are also Reading
- C++ & Python Program to find the Sum of Series 1/2+4/5+7/8+…
- Write a Program to sort an array using Bubble Sort
- C++ & Python Program for the Union of Two Arrays
- C Program to Convert Feet into Inches
- Program to Find the Sum of Series 1+x+x^2+……+x^n
- C++ Calculator to perform Arithmetic Operations using Switch Case
- Python RegEx
- Python Program to Swap Two Variables
- Program to find the average of list of numbers entered by the user
Leave a Comment on this Post