Get a Substring in C
Given a string str and pos and len that defines the starting and the length of the subarray. The task is to generate a substring of size len starting from the index pos.
A substring is a contiguous sequence of characters within a String.
Examples:
Input: Str =”the”, pos=1, len=2
Output: “th”
Explanation: substrings will be: “”, “t”, “h”, “e”, “th”, “he”, “the”.Input: Str =”geeks”, pos=3, length=3
Output: “eks”
Explanation: substrings are: “”, ” g”, “e”, “e”, “k”, “s”, “ge”, “ee”, “ek”, “ks”, “gee”, “eek”, “eks”, “geek”, “eeks”, “geeks”.
Approach: The problem can be solved following the below idea:
Create a character array and put the characters starting from pos to the character array to generate the substring.
Follow the below steps to implement the idea:
- Create a character array to store the substring.
- Iterate from the given position for the given length to generate the substring required.
- Then store each character in the character array and print the substring.
Follow the below illustration for a better understanding.
Illustration:
Consider a string str=”abcde” , pos = 2, len = 3.
=> At i = 2 our ans = “c”.
=> At i = 3, the character is ‘d’.
So add ‘d’ to the answer.
Our ans = “cd”=> At i = 4, the character is ‘e’.
So add ‘e’ to the answer.
Uur ans = “cde”.
Below is the implementation of the above approach.
C
// C implementation of code #include <stdio.h> #include <string.h> // Function to get substr in C void getString(int pos, int len, int c, char string[]) { char substring[1000]; while (c < len) { substring = string[pos + c - 1]; c++; } substring = '\0'; // Print the result printf(substring); printf("\n"); return 0; } // Driver code int main() { int pos, len, c = 0; // Testcase1 char string[14] = "geeksforgeeks"; // Initialize pos, len i.e., starting // index and len upto which we have to // get substring respectively. pos = 6; len = 5; printf("String: %s ", string); printf("\nsubstring is: "); // Function call getString(pos, len, c, string); // Testcase2 char string2[5] = "abcde"; pos = 1; len = 3; c = 0; printf("\nString: %s ", string2); printf("\nsubstring is: "); // Function call getString(pos, len, c, string2); return 0; }
String: geeksforgeeks substring is: forge String: abcde substring is: abc
Time complexity: O(len)
Auxiliary space: O(len)
Using strncpy() function in C
We can also use strncpy() function in C to copy the substring from a given input string. It takes 3 parameters which are the destination string, source string along with starting index and length of the substring which we need to copy.
Syntax:
strncpy(destination_string,input_string+pos,len);
Here pos is the starting index and len is the length of the substring which we want to copy.
Below is the code for the above approach.
C
// C implementation of code #include <stdio.h> #include <string.h> // Driver code int main() { int pos, len; // Testcase1 char string[14] = "geeksforgeeks"; char substring[14]; // Initialize pos, len i.e., starting // index and len upto which we have to // get substring respectively. pos = 6; len = 5; printf("String: %s ", string); printf("\nsubstring is: "); // Using strncpy function to // copy the substring strncpy(substring,string+(pos-1),len); printf(substring); // Testcase2 char string2[5] = "abcde"; char substring2[5]; pos = 1; len = 3; printf("\nString: %s ", string2); printf("\nsubstring is: "); // Using strncpy function to // copy the substring strncpy(substring2,string2+(pos-1),len); printf(substring2); return 0; } // This code is contributed by Pushpesh Raj.
String: geeksforgeeks substring is: forge String: abcde substring is: abc
Time complexity: O(len)
Auxiliary space: O(len)
Please Login to comment...