程序在C中复制字符串(Program to copy string in C)

优质
小牛编辑
135浏览
2023-12-01

实现 (Implementation)

现在,我们将看到该计划的实际执行情况 -

#include <stdio.h>
int main() {
   char s1[] = "TajMahal";       // String Given
   char s2[8];                   // Variable to hold value
   int length = 0;
   while(s1[length] != '\0') {
      s2[length] = s1[length];
      length++;
   }
   s2[length] = '\0';           // Terminate the string
   printf("Value in s1 = %s \n", s1);
   printf("Value in s2 = %s \n", s2);
   return 0;
}

输出 (Output)

该计划的输出应为 -

Value in s1 = TajMahal
Value in s2 = TajMahal