Write a program in that display a substring from a given string knowing its length and its starting position.
Example
Enter the string : my name is toto
Enter the position : 4
Enter the length of substring : 6
The substring retrieved from the string beginning from position 4 of length 8 is : "name i"
Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#define SIZE 100
void main()
{
char str[SIZE], substr[SIZE];
int pos, length, count;
printf("Enter the string : ");
gets(str);
printf("Enter the position :");
scanf("%d", &pos);
printf("Enter the length of substring :");
scanf("%d", &length);
count=0;
while (count < length && str[pos+count-1]!='\0')
{
substr[count] = str[pos+count-1];
count++;
}
substr[count] = '\0';
printf("The substring retrieved from the string beginning from position %d of length %d is : \"%s\"\n\n", pos, length, substr);
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Implementation of an array using a stack