【发布时间】:2019-02-14 12:02:33
【问题描述】:
我正在尝试使用指针和字符来反转此字符串,但我得到的输出缺少第二个字符并且找不到原因。就像在下面的情况下,我错过了 B 这个词。
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
int size = 100;
char oldText[size];
char newText[size];
char *pntr;
pntr = oldText;
cout << "Write the Text: \n";
for (int i=0; i<size; i++)
{
cin.get(pntr,size);
newText[i]=*pntr;
cout << *pntr;
pntr++;
}
cout << "The text backwards is: \n";
for (int i = size; i>=0; i--)
{
pntr--;
cout <<newText[i];
}
cout <<endl;
return 0;
}
【问题讨论】:
-
请不要在提问中使用图片。他们没用。而是尝试将图像的上下文粘贴到文本中。
-
istream::get (char* s, streamsize n)将n字符提取到s中,或者直到遇到分隔符。所以它会在一次调用中提取多达 100 个字符,你为什么要循环调用它?