void attachString(){
const int size = 100;
char string[size], string1[size], wholeString[2 * size + 2];
int i, j;
getString(string, size);
getString(string1, size);
for (i = 0; string[i] != '\0'; i++)
wholeString[i] = string[i];
wholeString[i++] = ' ';
for (j = 0; string1[j] != '\0'; j++)
wholeString[i++] = string1[j];
cout << wholeString << endl;
}
到目前为止,这是我的代码。 getString
函数在cin.ignore
之前调用getline
。
输出为 San rancisco ,而不是 San Francisco 。我想念什么?
答案 0 :(得分:-1)
在这里,我们创建一个数组,其大小等于两个数组的大小之和。 然后,将array1的所有元素复制到新数组,然后将数组2的所有元素复制到新数组。
int size1,size2;
cin>>size1>>size2;
char array1[size1], array2[size2];
for(int i=0;i<size1;i++)
{
cin>>array1[i];
};// entered elements into array1
for(int i=0;i<size2;i++)
{cin>>array2[i];
}
char new_array[size1+size2];//creating new array
for(int i=0;i<size1;i+++)
{new_array[i]=array1[i];
};
for(int i=0;i<size2;i++)// copying elements from array2
{
new_array[size1+i]=array2[i];
};
//All the elements are thus copied to new_array which is a concatination of both arrays array1 and array2.
答案 1 :(得分:-8)
void attachString(){
const int size = 100;
char string[size], string1[size], wholeString[2 * size + 2];
int i, j;
getString(string, size);
getString(string1, size);
memset(wholeString, 0, 2 * size + 2);
strncpy(wholeString, string, size);
wholeString[size] = ' ';
strncpy(&wholeString[size+1], string1, size);
cout << wholeString << endl;
}