More Reading in the Book: C++ Arrays
13/11/2010Today the “Book” asked me to create a program that would take a persons name sepparated into three arrays and merge them in the forth array. They call it concatenating strings to form one. The lesson was pretty strait forward, however, they expected me to set the strings within the code. I understood the simplicity behind it, but that counters the thoughts of programming in my eyes. I figured there was suppose to be some form of reusability, and they would try to teach that from the beginning. Anyhow, I created, altered it so that it would ask the user to input their name, and then it merges them together. I am beginning to understand a little more, and the process of creating small programs is starting to become more simple.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> #include <string.h> using namespace std; int main() { const int maxChar = 30; char firstName[maxChar] = {'\0'}; char middleName[maxChar] = {'\0'}; char lastName[maxChar] = {'\0'}; char fullName[93] = {'\0'}; int offset = 0; cout << "Input first name: "; cin >> firstName; cout << "\nInput middle name: "; cin >> middleName; cout << "\nInput laste name: "; cin >> lastName; cout << "\n" << endl; strcpy(fullName, firstName); offset = strlen(firstName); strcpy(fullName+offset," "); offset += 1; strcpy(fullName+offset,middleName); offset += strlen(middleName); strcpy(fullName+offset,". "); offset += 2; strcpy(fullName+offset, lastName); cout << "First name copied: " << firstName << endl; cout << "Middle name copied: " << middleName << endl; cout << "Last name copied: " << lastName << endl; cout << "char array fullName is: " << fullName << endl; return 0; } |