1.++ch,代表字符+1输出,比如输入是a,那么输出是b
#include<iostream>
using namespace std;
int main()
{
char ch;
cout << "输入,我将重复\n";
cin.get(ch);
while (ch != '.')
{
if (ch == '\n')
cout << ch;
else
cout << ++ch;//将ch往后加1输出字符
cin.get(ch);
}
cout <<"OK"<< endl;
system("pause");
return 0;
}
运行结果:

2.ch+1,代表字符+1后ASCII输出,比如输入是a,那么输出是98
#include<iostream>
using namespace std;
int main()
{
char ch;
cout << "输入,我将重复\n";
cin.get(ch);
while (ch != '.')
{
if (ch == '\n')
cout << ch;
else
cout << ch+1;
cin.get(ch);
}
cout <<"OK"<< endl;
system("pause");
return 0;
}
运行结果:

当然如果想输出原始ASCII值,可以ch+1-1,但是这样不如int(ch)方便
3.ch++,代表字符先输出,后加1,所以输入abc,输出还是abc
#include<iostream>
using namespace std;
int main()
{
char ch;
cout << "输入,我将重复\n";
cin.get(ch);
while (ch != '.')
{
if (ch == '\n')
cout << ch;
else
cout << ch++;
cin.get(ch);
}
cout <<"OK"<< endl;
system("pause");
return 0;
}
运行结果

有人问,那ch++没有任何作用吗?
回答是否定的!肯定有作用,我们加一句cout输出语句
#include<iostream>
using namespace std;
int main()
{
char ch;
cout << "输入,我将重复\n";
cin.get(ch);
while (ch != '.')
{
if (ch == '\n')
cout << ch;
else
cout << ch++;
cout << " ch++ " << ch <<endl;
cin.get(ch);
}
cout <<"OK"<< endl;
system("pause");
return 0;
}


本文深入探讨了C++中字符操作的三种不同方式:通过++ch实现字符的直接递增输出,通过ch+1获得字符的ASCII值加一,以及通过ch++进行先输出后递增的操作,并附带代码示例及运行结果。
2716





