str_cpy1.cpp

来自「数据结构常用算法合集」· C++ 代码 · 共 32 行

CPP
32
字号
 //str_cpy1.cpp
 #include <iostream.h>				//cin,cout
 #include <conio.h>				//getch()
 #include <string.h>
 void main()
 { char *source="America apologize!",*destination;
   int len;
   len = strlen(source)+1;			//取得字符串长度加1
   destination = new char[len];		//配置内存
   strcpy(destination,source);		//拷贝字符串
   cout << "用strcpy()全部拷贝后字符串为:"
       << destination << endl;
   strcpy(destination,"");			//清除字符串
   cout << "用strcpy()清除字符串后:"
       << destination << endl;
   stpcpy(destination,source);		//拷贝字符串
   cout << "用stpcpy()全部拷贝后字符串为:"
       << destination << endl;
   stpcpy(destination,"");			//清除字符串
   cout << "用stpcpy()清除字符串后:"
       << destination << endl;
   strncpy(destination,source,7);		//拷贝部分字符串
   destination[7]='\0';			//加上结尾
   cout << "用strncpy()拷贝部分后字符串为:"
       << destination << endl;
   destination[0]='\0';
   cout << "用\\0清除字符串后:"
 		 << destination << endl;
   delete[] destination;
   getch();
 }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?