Wednesday 18 December 2013

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

#include < stdio.h>
#include < string.h>
 
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 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 ! 

For More Details Please Visit Ictjobs.info

No comments: