C Program to Insert Character/Word in Any Desired Location in a String

This C Program inserts character/word in any desired location in a string.

Here is source code of the C Program to insert character/word in any desired location in a string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Insert Character/Word in any Desired Location 
  3.  * in a String 
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. void main()
  9. {
  10.     int i, j, count = 0, pos, flag = 0;
  11.     char s1[100], s2[10], s3[100];
  12.     char *ptr1, *ptr2, *ptr3;
  13.  
  14.     printf("\nenter the String:");
  15.     scanf(" %[^\n]s", s1);
  16.     printf("\nenter the string to be inserted:");
  17.     scanf(" %[^\n]s", s2);
  18.     printf("\nenter the position you like to insert:");
  19.     scanf("%d", &pos);
  20.  
  21.     ptr1 = s1;
  22.     ptr3 = s3;
  23.     /*COPYING THE GIVEN STRING TO NEW ARRAY AND INSERTING THE STRING IN NEW ARRAY*/
  24.     for (i = 0, j = 0;*ptr1 != '\0'; ptr1++, i++, j++, ptr3++)
  25.     {
  26.         s3[j] = s1[i];
  27.         if (*ptr1 == ' ' && flag != 1)
  28.             ++count;
  29.         if (flag != 1 && count == pos - 1)
  30.         {
  31.             flag = 1;
  32.             for(ptr2 = s2;*ptr2 != '\0'; ptr2++)
  33.             {
  34.                 s3[++j] = *ptr2;
  35.                 ptr3++;
  36.             }
  37.             s3[++j] = ' ';
  38.             ptr3++;
  39.         }
  40.     }
  41.     s3[j] = '\0';
  42.     printf("\nthe string after modification is\n\n %s\n", s3);
  43. }

$ cc string10.c
$ a.out
enter the string:Welcome to Sanfoundry's C Programming Class,  Welcome Again to C Class!
enter the word to insert:Sanfoundry's
enter the position you like to insert:3
the string after modification is
 
Welcome to Sanfounsry's Sanfoundry's C Programming Class,  Welcome Again to C Class!

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.