Quiz #8 Strings Objective Write a program that copies strings without using the
ID: 3595673 • Letter: Q
Question
Quiz #8 Strings
Objective
Write a program that copies strings without using the string object. It will accomplish this by doing the following:
1. At start, the program creates two arrays of 50 characters, called text and copy.
2. Program prompts the user to “Enter a line”.
3. A line of input is collected from the user. The user’s input is stored in text as a C-string.
4. The contents of text is displayed to the console.
5. The CopyString function is called to copy the contents of text into copy.
6. Finally, the program outputs the contents of copy to the console.
You may not use notes or other sources of information, but you may use the compiler to check your work. Turn in your completed .cpp file to TITANium to receive credit.
Requirements
Your program must have the following attributes:
1. It must contain a function you create, called CopyString, that takes in two character arrays as parameters. The first parameter is source, the C-string to copy. The second parameter is target, the array the C-String contents of source will be copied into.
2. CopyString should be implemented so that only the C-String portion of source is copied to target.
3. Your program must not contain the <string> or <cstring> include files.
Hint
Consider using cin.getline (char[], int) to get a line of text from the user.
Expected Output
Enter a line:Hi, I'm a string.
text string: Hi, I'm a string.
copy string: Hi, I'm a string.
Please use #include <iostream> and using namespace std;
Explanation / Answer
#include <iostream>
using namespace std;
void CopyString(char source[], char destination[]) {
for(int i=0;source[i] != '';i++){
destination[i]=source[i];
}
}
int main()
{
char text[50], copy[50];
cout<<"Enter a line: "<<endl;
cin.getline (text, 50);
cout<<"text string: "<<text<<endl;
CopyString(text, copy);
cout<<"copy string: "<<copy<<endl;
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.