hour15_4.cpp
来自「《24学时精通c++》的随书源码的下半部分。欢迎下载学习。」· C++ 代码 · 共 20 行
CPP
20 行
// Listing 15.9 Using strncpy()
#include <iostream>
#include <string.h>
int main()
{
const int MaxLength = 80;
char String1[] = "No man is an island";
char String2[MaxLength+1];
strncpy(String2,String1,5); // limited to 5 rather than MaxLength
String2[strlen(String1)] = '\0'; // add a null to the end
std::cout << "String1: " << String1 << std::endl;
std::cout << "String2: " << String2 << std::endl;
// you should see how strncpy can limit the length that you copy to a smaller
// portion of the string. This can come in handy when you want to subset something
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?