#include <stdio.h> #include <string.h> int main() { char str1[100]; char str2[100]; printf("(1) input str1: "); scanf_s("%s", str1, sizeof(str1)); printf(" input str2: "); scanf_s("%s", str2, sizeof(str2)); strcpy_s(str2,100,str1); printf("(2) exchange str1=%s str2=%s\n", str1, str2); printf("len1=%d len2=%d\n", strlen(str1), strlen(str2)); i would change str1 to str2 each other.
At first i'd written 'strcpy_2(str2,str1)', But VS said i need more parameter, so i wrote 'strcpy_s(str2,100,str1).
Although error isn't founded, the result is wrong. str1 and str2 has same value about str1.
What is my problem? Thank you.
memcpyjust to do that.str2to a temporary string, copystr1tostr2, then copy the temporary string tostr1. That's the usual method for any kind of swap.