Доброе время суток. Если не трудно, объясните, пожалуйста, почему
работает эта программа.
// Concatenate - concatenate two strings
// with a " - " in the middle
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
// prototype declarations
void concatString(char szTarget[], const char szSource[]);
int main(int nNumberofArgs, char* pszArgs[])
{
// read first string...
char szString1[256];
cout << "Enter string #1:";
cin.getline(szString1, 128);
// ...now the second string...
char szString2[128];
cout << "Enter string #2:";
cin.getline(szString2, 128);
// ...concatenate a " - " onto the first...
concatString(szString1, " - ");
// ...now add the second string...
concatString(szString1, szString2);
// ...and display the result
cout << "\n" << szString1 << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
return 0;
}
// concatString - concatenate the szSource string
// onto the end of the szTarget string
void concatString(char szTarget[], const char szSource[])
{
// find the end of the first string
int targetIndex = 0;
while(szTarget[targetIndex])
{
targetIndex++;
}
// tack the second onto the end of the first
int sourceIndex = 0;
while(szSource[sourceIndex])
{
szTarget[targetIndex] =
szSource[sourceIndex];
targetIndex++;
sourceIndex++;
}
// tack on the terminating null
szTarget[targetIndex] = '\0';
}
Эта программа из учебника. Она соединяет две строки символов. Если ввести последовательно "движение" и "жизнь", она выдаст "движение - жизнь". Если я правильно понимаю, циклы while(szTarget[targetIndex]) и while(szSource[sourceIndex]) выполняются до тех пор, пока значения szTarget[targetIndex] и соответственно szSource[sourceIndex] не будут равны нулю. Но на самом деле если ввести две строчки состоящие только из нулей, например "00" и "000" программа выдаст "00 - 000". Я видимо что-то не так понимаю.
Вопрос, наверно, слишком, простой, но в описании раздела написано "Всё, что касается программирования"...