2018年7月21日 星期六

Using C-style Strings

#include <iostream>
#include <cstring>
using namespace std;

void strings_and_functions() {
   
    //----WRITE YOUR CODE BELOW THIS LINE----
    char whole_name[50] {}, first_name[50] {"Bjarne"}, last_name[50] {"Stroustrup"};
    int first_name_length{}, last_name_length{}, whole_name_length{};
   
   
    first_name_length = strlen(first_name);
    last_name_length = strlen(last_name);
    strcpy(whole_name, first_name);
    strcat(whole_name, last_name);
    whole_name_length = strlen(whole_name);
   
    //----WRITE YOUR CODE ABOVE THIS LINE----
    //----DO NOT MODIFY THE CODE BELOW THIS LINE----
   
    cout << "The length of the first name, " << first_name << ", is " << first_name_length << " letters long and the length of the last name, " << last_name << ", is " << last_name_length << " letters long. This means that the length of the whole name must be " << whole_name_length << " letters long.";
}

沒有留言:

張貼留言