- 论坛徽章:
- 1
|
小乔大概写程序都不测试结果的。
已经达到随手写、随手发布的境界。
http://chenlq.net/dev/cpp-why/57 ... time-and-space.html
最后这段程序,我怎么看怎么不对,运行试下,果然。
拍照留恋如下:
- #include <iostream>
- #include <cstring>
- char* movestr(char* str,const int n)
- {
- const int LEN = strlen(str);
- // 申请字符串长度的内存作为临时中转交换
- char* p = new char[LEN+1];
- if(nullptr != p)
- {
- // 使用memcpy()代替逐个字符的复制移动
- memcpy(p,str+n,LEN-n);
- memcpy(p+LEN-n,str,n);
- p[LEN]='\0';
- // 复制回原字符串
- strcpy(str,p);
- delete[] p;
- p = nullptr;
- }
- return str;
- }
- int main()
- {
- char str[] = "today is sunday";
- std::cout<<movestr(str,3)<<std::endl;
- return 0;
- }
复制代码 |
|