adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C to insert a character in string at given position

Program in C to insert a character in string at given position








              NOTE :- In insertion operation  str[ length +1 ] = '\0' means the string length always end with null (\0).



#include<stdio.h>
#include<string.h>
 int main()
  {
   char str[100],data;
   int position,i,length=0;
   printf("Enter a string:-");
   scanf("%s",str);
   printf("Enter a character and position where we would data insert. :-");
   fflush(stdin);
   scanf("%c%d",&data,&position);
   printf("\nBefore inserrtion = %s\n",str);
   length=strlen(str);
   for(i=length;i>=position-1;i--)
    { 
        str[i]=str[i-1];
      }
      str[position-1]=data;
      str[length+1]='\0';
   printf("\nAfter inserrtion = %s",str);  return 0;
  }
Output:-








Related Programs

  1. Program in C to delete a Character from a given position in a given string

Post a Comment

1 Comments

Anonymous said…
TY MAN