Get a Substring in C Last Updated : 06 Dec, 2024 Comments Improve Suggest changes Like Article Like Report A substring is a contiguous sequence of characters within a string. In this article, we will learn how to extract a substring using a C program.The simplest method to get a substring from a larger string is by using strncpy() function. Let’s take a look at an example: C++ #include <stdio.h> #include <string.h> int main() { char s[] = "Hello, Geeks!"; int pos = 7, l = 5; // Char array to store the substring char ss[20]; // Extract substring of length 5 and from index 7 // using strncpy strncpy(ss, s + 7, 5); // Null terminate the substring ss[5] = '\0'; printf("%s", ss); return 0; } OutputGeeksExplanation: The strncpy() function copies a fixed number of characters from the source string to the destination substring array but we have to manually add a null terminator to the result.There are also a few other methods to extract the substring of specified length from the given position in a larger string in C. Some of them are as follows:Manually Using LoopIterate the string from the given position using a loop and copy the specified number of characters from this position to the substring. C #include <stdio.h> void getSub(char *s, char *ss, int pos, int l) { int i = 0; // Copy substring into ss while (i < l) { ss[i] = s[pos + i]; i++; } // Null terminate the substring ss[i] = '\0'; } int main() { char s[] = "Hello, Geeks!"; // Char array to store the substring char ss[20]; // Extract substring starting from // index 7 with length 5 ("Geeks") getSub(s, ss, 7, 5); printf("%s", ss); return 0; } OutputGeeksUsing PointersThe above algorithm can also be applied using pointers. Move the pointer to point to the starting position of the substring and copy the specified characters. C #include <stdio.h> void getSub(char *s, char *ss, int pos, int l) { int i = 0; // Move pointer to the pos s += pos; // Copy substring of lenght l while (l--) *ss++ = *s++; // Null terminate the string *ss = '\0'; } int main() { char s[] = "Hello, Geeks!"; int pos = 7, l = 5; // Char array to store the substring char ss[20]; // Extract substring starting from // index 7 with length 5 ("Geeks") getSub(s, ss, 7, 5); printf("%s", ss); return 0; } OutputGeeks Comment More infoAdvertise with us Next Article Get a Substring in C S soumyadeeppaul022 Follow Improve Article Tags : C Language Technical Scripter 2022 substring Similar Reads Bash Scripting - Substring sIn this article, we will discuss how to write a bash script to extract substring from a string. Extracting an Index-Based Substring There are various ways to obtain substring based on the index of characters in the string: Using cut commandUsing Bash substringUsing expr substr commandUsing awk comm 4 min read Substring meaning in DSA A substring is defined as a contiguous part of a string, i.e., a string inside another string. Substrings of String "geeks"Characteristics of Substring: Starting position of a substring is greater than or equal to the starting index of the string and the ending position is less than or equal to the 2 min read substr() in C++ In C++, the string substr() function is used to extract a substring from the given string. It generates a new string with its value initialized to a copy of a sub-string of the given string.Example:C++#include <iostream> #include <string> using namespace std; int main() { // Take any str 3 min read Strings in C A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'.DeclarationDeclaring a string in C i 5 min read String find() in C++ In C++, string find() is a built-in library function used to find the first occurrence of a substring in the given string. Letâs take a look at a simple example that shows the how to use this function:C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Welcome to GfG!"; s 4 min read Like