Get a Substring in C
Last Updated :
22 Dec, 2022
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
#include <stdio.h>
#include <string.h>
void getString( int pos, int len, int c, char string[])
{
char substring[1000];
while (c < len) {
substring = string[pos + c - 1];
c++;
}
substring = '\0' ;
printf (substring);
printf ( "\n" );
return 0;
}
int main()
{
int pos, len, c = 0;
char string[14] = "geeksforgeeks" ;
pos = 6;
len = 5;
printf ( "String: %s " , string);
printf ( "\nsubstring is: " );
getString(pos, len, c, string);
char string2[5] = "abcde" ;
pos = 1;
len = 3;
c = 0;
printf ( "\nString: %s " , string2);
printf ( "\nsubstring is: " );
getString(pos, len, c, string2);
return 0;
}
|
Output
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
#include <stdio.h>
#include <string.h>
int main()
{
int pos, len;
char string[14] = "geeksforgeeks" ;
char substring[14];
pos = 6;
len = 5;
printf ( "String: %s " , string);
printf ( "\nsubstring is: " );
strncpy (substring,string+(pos-1),len);
printf (substring);
char string2[5] = "abcde" ;
char substring2[5];
pos = 1;
len = 3;
printf ( "\nString: %s " , string2);
printf ( "\nsubstring is: " );
strncpy (substring2,string2+(pos-1),len);
printf (substring2);
return 0;
}
|
Output
String: geeksforgeeks
substring is: forge
String: abcde
substring is: abc
Time complexity: O(len)
Auxiliary space: O(len)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...