Extract a substring (substr) of length n from a string (str) from specified position (pos)
#include<stdio.h>
void main()
{
char
str[20], substr[20];
int
n, pos, i, j=0;
printf("\nEnter
the string:");
gets(str);
printf("\nEnter
the position:");
scanf("%d",
&pos);
printf("\nEnter
the length of substring:");
scanf("%d",
&n);
i
= pos;
while(str[i] != '\0' &&
n>0)
{
substr[j]
= str[i];
i++;
j++;
n--;
}
substr[j]
= '\0';
printf("\nThe
substring is:");
puts(substr);
}
Output:
Enter the
string: hello hii abc
Enter the
position: 2
Enter the
length of substring: 5
The
substring is: llo h
No comments:
Post a Comment