C program to fetch a sub-string from a string

c programming tutorial to convert string to fetch a sub-string from a string program

sub_string.c
#include <stdio.h>
 
int main() 
{
   char string[1000], sub[1000];
   int position, length, c = 0;
 
   printf("Input a string\n");
   gets(string);
 
   printf("Enter the position and length of substring\n");
   scanf("%d%d", &position, &length);
 
   while (c < length) {
      sub[c] = string[position+c-1];
      c++;
   }
   sub[c] = '\0';
 
   printf("Required substring is \"%s\"\n", sub);
 
   return 0;
}


Output-


Input a string
c programming
Enter the position and length of substring
3
11
Required substring is "programming"

1 comments

  1. Good
    Here is blog for basic C and C++ programs
    List of C and C++ Programs
    blog link
    C and C++ based programs

    ReplyDelete

My Instagram