C Program To Insert Substring Into A String

preview_player
Показать описание
C Program to INSERT a Sub-String in Main String at Given Position
Inserting characters into a string
C Program to Insert Character/Word in any Desired Location
Рекомендации по теме
Комментарии
Автор

There is more simple solution for this.


#include<stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
{
char strA[20] = "Blahblahblah", strB[5] = "123", strC[50];
int x = 4; //You declare an integer for how many characters you wish to insert the second string into the first
strncpy(strC, strA, x);
strC[x] = '\0';
strcat(strC, strB);
strcat(strC, strA + x); //Joint strC with strA(after 4th position/index 3)
printf("%s\n", strC); //print the string
return 0;
}

RakibHasan-
Автор

Hi, thanks for the video.
I compiled this program, one observation is, if position is 6it doesn't add space at the end of new word..example below
inputs: I am indian, an, 6.
output: I am anindian instead of I am an indian.
could you please add this piece of code..

nirmalah