Contact Learn C
Copy

Program 269: Program to print 'N' Characters from the given Position of a String using Pointers

Program 269: Program to print 'N' Characters from the given Position of a String using Pointers

#include<stdio.h>
#include<string.h>
main()
{
  char s1[25],*t,s2[25];
  int noofCharacters,pos,i=0,k=0;
  printf("Enter a string\n");
  gets(s1);
  printf("Enter Position(counting from zero)from where string has to be copied(includes space)\n");
  scanf("%d",&pos);
  printf("Enter No of characters from the position %d to be copied to another string\n",pos);
  scanf("%d",&noofCharacters);
  t=s1;//copying base address of string
  while(*t!='\0')
  {
   if(i>=pos && noofCharacters!=0)
   {
    s2[k]=*t;
    k++;
    noofCharacters--;
   }
   i++;
   t++;
  }
  s2[k]='\0';
  printf("Copied String is(includes space also) %s\n",s2);
}
Explanation:

//Coming Soon

Output:

Program to print 'N' Characters from the given Position of a String using Pointers

Program to print 'N' Characters from the given Position of a String using Pointers


Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide