我们可以使用内置的 strcpy()函数 要将一个字符串复制到另一个字符串,但在这里,该程序不使用strcpy()函数手动将一个字符串的内容复制到另一个字符串。
null
方法: 这里我们在输入中给出一个字符串,然后在 循环 我们将第一个数组的内容转移到第二个数组。
错误: 如果目标字符串长度小于源字符串,则整个字符串值不会复制到目标字符串中。 例如,考虑目标字符串长度为20,源字符串长度为30。然后,源字符串中只有20个字符将被复制到目标字符串中,其余10个字符将被截断。
// CPP program to copy one string to other // without using in-built function #include <stdio.h> int main() { // s1 is the source( input) string and s2 is the destination string char s1[] = "GeeksforGeeks" , s2[100], i; // Print the string s1 printf ( "string s1 : %s" , s1); // Execute loop till null found for (i = 0; s1[i] != ' ' ; ++i) { // copying the characters by // character to str2 from str1 s2[i] = s1[i]; } s2[i] = ' ' ; // printing the destination string printf ( "String s2 : %s" , s2); return 0; } |
输出:
string s1 : GeeksforGeeks String s2 : GeeksforGeeks
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END