Extract substring from a string using pointers
C program to extract substring of n characters starting from the given position using pointers
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char str1[MAX], str2[MAX];
char *ptr1, *ptr2;
int pos, n, i;
printf("Enter any string\t:");
gets(str1);
printf("Enter the position from where substring is to be extracted\t:");
scanf("%d",&pos);
printf("Enter number of characters to be extracted\t:");
scanf("%d",&n);
ptr1 = str1;
ptr2 = str2;
for(i=0;i<pos;i++)
{
ptr1++;
}
for(i=0;i<n;i++)
{
*ptr2 = *ptr1;
ptr1++;
ptr2++;
}
*ptr2 = '\0';
printf("\nExtracted substring\t:%s",str2);
getch();
}
Output
Enter any string :welcome to coursecrux.com
Enter the position from where substring is to be extracted :4
Enter number of characters to be extracted :6
Extracted substring :ome to
Explanation
In the above program, a string is taken as an input from the user, and stored in some variable named say 'str1'. Another variable 'str2' is also taken, in which extracted substring from the string 'str1' is stored. Starting position from where substring is to be extracted is taken as an input from the user and stored in the variable named say 'pos'. Number of characters to be extracted is stored in the variable named 'n'. Two pointers 'ptr1' and 'ptr2' are taken that points to 'str1' and 'str2' respectively. 'ptr1' and 'ptr2' points at the first character of their respective strings using the statement
To extract the substring, first we have to traverse the string 'str1', and take the pointer 'ptr1' to the position 'pos'.
< Now, starting from position 'pos', 'n' number of characters are to be copied in string 'str2'.
'ptr1' is pointing to character at 'pos' position of 'str1' and 'ptr2' is pointing at the beginning of string 'str2'. character from 'str1' is copied to 'str2' using the statement
Now, both the pointers 'ptr1' and 'ptr2' are increment by 1, so that they point to the next character which is to be copied and next location where that character is to be copied.
This complete process (above two steps) is repeated 'n' number of times.
At the end of the loop, 'str2' contains the extracted substring.
Each string is NULL terminated. So, 'str2' string is also NULL terminated explicitly using the statement,
ptr1 = str1;
ptr2 = str2;
To extract the substring, first we have to traverse the string 'str1', and take the pointer 'ptr1' to the position 'pos'.
for(i=0;i<pos;i++)
{
ptr1++;
}
< Now, starting from position 'pos', 'n' number of characters are to be copied in string 'str2'.
'ptr1' is pointing to character at 'pos' position of 'str1' and 'ptr2' is pointing at the beginning of string 'str2'. character from 'str1' is copied to 'str2' using the statement
*ptr2 = *ptr1;
Now, both the pointers 'ptr1' and 'ptr2' are increment by 1, so that they point to the next character which is to be copied and next location where that character is to be copied.
ptr1++;
ptr2++;
This complete process (above two steps) is repeated 'n' number of times.
At the end of the loop, 'str2' contains the extracted substring.
Each string is NULL terminated. So, 'str2' string is also NULL terminated explicitly using the statement,
*ptr2 = '\0';