top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Write a C program to insert character/word in any desired location in a string?

+2 votes
1,191 views

Output
Enter the string: Welcome to C Class!
Enter the word to insert: programming
Enter the position you like to insert: 3
The string after modification is

Welcome to C programming Class!

posted May 19, 2017 by Pooja Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Please go through this source code 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);
} 
answer May 20, 2017 by Naziya Raza Khan
0 votes
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX_STRING_LENGTH 100
int main()
{
        char str1[MAX_STRING_LENGTH],str2[MAX_STRING_LENGTH];
        int pos;
        printf("Enter main string : ");
        scanf("%[^\n]%*c",str1);
        printf("Enter sub string : ");
        scanf("%[^\n]%*c",str2);
        printf("Enter position : ");
        scanf("%d",&pos);
        char *ptr=NULL;
        ptr=(char*)malloc(strlen(str1)+strlen(str2)+2);
        if(ptr==NULL)
        {
                printf("Unable to allocate memory\n");
                exit(1);
        }
        int totalPosition=0,i=0,j=0,count=0;
        for(;str1[i];i++)
        {
                if(str1[i]==' ')
                        totalPosition++;
        }
        if(pos==0)
        {
                strcpy(ptr,str2);
                strcat(ptr," ");
                strcat(ptr,str1);
        }
        else if(pos > totalPosition)
        {
                strcpy(ptr,str1);
                strcat(ptr," ");
                strcat(ptr,str2);
        }
        else
        {
                for(i=0;str1[i];i++)
                {
                        ptr[j++]=str1[i];
                        if(str1[i]==' ')
                        {
                                count++;
                                if(count==pos)
                                {
                                        int k=0;
                                        for(;str2[k];k++)
                                                ptr[j++]=str2[k];
                                        ptr[j++]=' ';
                                }
                        }
                }
        }
        printf("String is %s\n",ptr);
        free(ptr);
}
answer May 23, 2017 by Chirag Gangdev
Similar Questions
0 votes

Enter character: r
Enter string: programming
Output: Positions of 'r' in programming are: 2 5
Character 'r' occurred for 2 times

+2 votes

If i am giving input-- welcome to c programming language, c programming language by E. Balagurusamy !
The output will be-- welcome to c programming language, by E. Balagurusamy !

–1 vote

Write a C program to check if the given string is repeated substring or not.
ex
1: abcabcabc - yes (as abc is repeated.)
2: abcdabababababab - no

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...