有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
Is there a way to use strcpy to copy a string array into another string or different array?
又是我!
我仍然是C++新手,昨天刚刚提交了一份作业,创建了一个包含 7 个选项可供选择的菜单。我能够完成除选项 7 之外的所有必需任务,该选项 7 将一个数组复制到另一个数组并处理新数组。
我知道我的代码在案例 7 中没有显示它,但是有没有人可以让我知道我将如何使用我拥有的当前代码完成任务?它不会再帮助我完成任务了,但我觉得这是我应该能够做的事情。也许我在屏幕前花了太长时间,寻找它只是躲避我的答案......在任何情况下,我将不胜感激我能为我未来的计划获得的任何帮助。
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main(void)
{
// Delcarations
string MasterFile[6]; // MasterFile Array
MasterFile[0] = "I want to thank all the C++ students who has helped me this semester. You have inspired me to work harder and to be able to design small C++ programs using a gamming approach.";
string master = MasterFile[0];
string str1 = "my Instructor, Professor Penn";
string str2 = "I want to thank all the C++ students who has helped me this semester. You have inspired me to work harder and to be able to design efficient C++ programs using a gamming approach.";
char masterfile[] = { "I want to thank all the C++ students who has helped me this semester. You have inspired me to work harder and to be able to design small C++ programs using a gamming approach." };
char StudentFile[1] = {masterfile[1]}; // StudentFile Array
char selection;
// Defines the width of the menu
const int menuWidth = 84;
// This is a loop structure for the menu box using ASCII
// This prints the top left corner of the menu box (ASCII)
cout << char(201);
// This prints the top border of the menu box (ASCII)
for (int column = 0; column < menuWidth; ++column) {
cout << char(205);
}
// This prints the top right corner of the menu box (ASCII)
cout << char(187);
cout << "n";
// array with menu options
string menu[20];
menu[0] = " **************";
menu[1] = " *** MAIN MENU ***";
menu[2] = " **************";
menu[3] = " Please Choose From The Following Options:";
menu[6] = " 1. Master File Array Statement:";
menu[8] = " 2. Length of Master File Array:";
menu[10] = " 3. Replacing Phrase 'all the C++ students' with 'my Instructor, Professor Penn':";
menu[12] = " 4. Swaping 'small' with 'efficient':";
menu[14] = " 5. Find and Display the Position of 'th': ";
menu[16] = " 6. Erase the Phrase 'using a gaming approach':";
menu[18] = " 7. Copy MasterFile Array to Student Array:";
for (string option : menu)
{
cout << char(186) // print left border
<< setw(menuWidth) // set next item width
<< left // set next item aligment
<< option // print menu option string with width and aligment
<< char(186) << "n"; // print right border
}
// This will print the bottom left corner of the menu box (ASCII)
cout << char(200);
// This prints the bottom border of the menu box (ASCII)
for (int column = 0; column < menuWidth; ++column) {
cout << char(205);
}
// This prints the bottom right corner of the menu box (ASCII)
cout << char(188);
cout << "nn";
/*
END OF MENU
*/
cout << "ttt Enter Your Selection: ", cin >> selection, cout << endl;
do
{
switch (selection)
{
case '1':
cout << "You have chosen to create Masterfile Array:nn";
cout << master; cout << endl;
cout << "nn";
break;
case '2':
cout << "You have chosen to display the length:nn";
cout << "The number of characters in MasterFile Array is: ";
cout << master.length(), cout << endl;
cout << "nn";
break;
case '3':
cout << "You have chosen to replace 'all the C++ students' with 'my Intructor, Professor Penn':nn";
cout << master.replace(16, 20, str1), cout << endl;
cout << "nn";
break;
case '4':
master.swap(str2);
cout << "You have to swap the word 'small' with 'efficient':nn";
cout << master; cout << "nn";
break;
case '5':
cout << "You have chosen to find and display the 'th' position:nn";
cout << "'th' starts in position: ";
cout << master.find("th"); cout << " of the array."; cout << endl;
cout << "nn";
break;
case '6':
cout << "You have chosen to erase the phrase 'using a gaming approach:nn";
cout << master.erase(149) << ".", cout << "nn";
break;
case '7':
cout << "nYou have chosen to copy StudentFile Array into MasterFile Array:nn";
cout << master;
default: cout << "n Invalid selectionnn";
}
} while ((selection = main()) != 7);
return 0;
}
方法
1:最简单的方法
如此 StackOverflow 问题的第一个答案中所述,如果您使用的是 C++11,则可以使用std::array
将一个数组复制到另一个数组。您完全不必担心使用strcpy
。
std::array<int,4> A = {1,2};
std::array<int,4> B = A; // This copies array A into array B.
因此,对于您的特定代码,您只需创建一个新的字符串数组并将其设置为等于master
。
以下是std::array
的相关文档。
方法 2:使用 strcpy
或者,如果您确实想使用strcpy,
请使用strcpy_s
,因为 strcpy 现在被认为是不安全且过时的功能。你可以这样称呼它:
strcpy_s(FirstThing, SecondThing);
这会将 SecondThing 的内容复制到 FirstThing 中。
相关文章:
- 使用 jsoncpp 解析 json 数组字符串
- C++ 对象数组字符串输入在控制台上不起作用
- 插入了 C++ 数组字符串数据,但在显示输出时不显示
- 无法区分 JSON 对象/数组/字符串
- 为什么数组(字符串类型)的大小是 24 字节,带有单个空格元素
- 如何使用数组字符串进行输入验证
- (C++) 打印字符数组字符串的单词时出现问题
- 使用Back_inserter从任何位置的输入字符中使用Back_inserter进行过滤数组字符串
- C 函数反转字符数组字符串
- 如何在 c++ 中创建数组(字符串 [] )的向量
- 如何返回数组字符串的指针
- char数组字符串混淆
- 如何从字符数组字符串中提取空格
- 是否可以将 Bitset<8> 的值复制到数组字符串而不转换它们?
- C++数组字符串函数
- 如何在函数中操作字符数组(字符串)的指针,该指针在C / C ++中作为参数传递
- visual将音频文件存储到数组/字符串流C++中
- c++在char数组(字符串)上将what替换为
- 将数组字符串转换为intger c++(初学者)
- 需要设置数组字符串长度,使其恰好为13