Saturday 11 November 2017

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


Code:

#include
#include

void main()
{
    int i, j, count = 0, pos, flag = 0;
    char s1[100], s2[10], s3[100];
    char *ptr1, *ptr2, *ptr3;

    printf("\nenter the String:");
    scanf(" %[^\n]s", s1);
    printf("\nenter the string to be inserted:");
    scanf(" %[^\n]s", s2);
    printf("\nenter the position you like to insert:");
    scanf("%d", &pos);

    ptr1 = s1;
    ptr3 = s3;
    /*COPYING THE GIVEN STRING TO NEW ARRAY AND INSERTING THE STRING IN NEW ARRAY*/
    for (i = 0, j = 0;*ptr1 != '\0'; ptr1++, i++, j++, ptr3++)
    {
        s3[j] = s1[i];
        if (*ptr1 == ' ' && flag != 1)
            ++count;
        if (flag != 1 && count == pos - 1)
        {
            flag = 1;
            for(ptr2 = s2;*ptr2 != '\0'; ptr2++)
            {
                s3[++j] = *ptr2;
                ptr3++;
            }
            s3[++j] = ' ';
            ptr3++;
        }
    }
    s3[j] = '\0';
    printf("\nthe string after modification is\n\n %s\n", s3);
}


Output:

enter the string:Welcome to ExecuteCodes C Programming Class,  Welcome Again to C Class!
enter the word to insert:ExecuteCodes
enter the position you like to insert:3
the string after modification is

Welcome to ExecuteCodes ExecuteCodes C Programming Class,  Welcome Again to C Class!


More C Programs:











100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...