Write a program that extracts part of the given string from the specified position. For example, if the sting is "Working with strings is fun", then if from position 4, 4 characters are to be extracted then the program should return string as "king". Moreover, if the position from where the string is to be extracted is given and the number of characters to be extracted is 0 then the program should extract entire string from the specified position.

 #include <stdio.h>


int

main ()

{

  char n[50];

  int pos, ext;

  printf ("Enter name \n");

  scanf ("%s", n);

  printf ("\nEnter position from where you want to extract string ");

  int len = strlen (n);

  

  

  while (1)

    {

      printf ("\nPosition should be less then %d\n", len);

      scanf ("%d", &pos);

      if (pos <= len)

break;

      else

printf ("\nNot a valid Position enter again");

    }

    

    

     printf("\nEnter number of characters you want to extract from %d position.", pos);

     printf("extraction number should be less than %d\n", len-pos);

     scanf ("%d", &ext);


  if (ext == 0)

    {

        for (int i = pos - 1; i < len; i++)

             printf ("%c", n[i]);

    }

         for (int i = pos - 1; i < pos + ext - 1; i++)

             printf ("%c", n[i]);

  return 0;

}

Comments