+ 2

how could we merge a string into another string within specified position??

14th Feb 2019, 3:34 AM
Janak Sharma
Janak Sharma - avatar
2 Answers
+ 8
When you say "merge" do you mean to append (at the end) or to copy? If you mean to copy you can try strncpy() http://www.cplusplus.com/reference/cstring/strncpy/ If you mean to append, then use strncat() http://www.cplusplus.com/reference/cstring/strncat/ An example using strncpy() function: #include <stdio.h> #include <string.h> int main() { char str1[25] = "SoloStudy"; int length = (int)strlen(str1); printf("Before: %s (%d characters)\n", str1, length); // Copy the string "Learn" into str1 // starting from offset of str1 + 4 // 5 characters will be copied over. strncpy(str1 + 4, "Learn", 5); length = (int)strlen(str1); printf("After: %s (%d characters)\n", str1, length); return 0; } Hth, cmiiw
14th Feb 2019, 4:06 AM
Ipang
0
but this truncated the study part i am asking let "hel there " be the string and we want to add "lo" after 3rd character such that string becomes"hello there"
14th Feb 2019, 2:55 PM
Janak Sharma
Janak Sharma - avatar