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

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.

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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.