如何在c++中解决这个输出

how to solve this output in C++

本文关键字:输出 解决 c++      更新时间:2023-10-16
#‎include‬ <iostream>
#include <ctype.h>
using namespace std;
typedef char Txt80[80];
int main()
{
 char *PText;
 Txt80 Txt = "Ur2GReAt";
 int N = 6;
 PText = Txt;
 while (N >=3)
 {
  Txt[N] = (isupper(Txt[N] ? tolower(Txt[N]):toupper(Txt[N])));
  cout << PText <<endl;
  N--;
  PText++;
 }
}
Output is :
 Ur2GRe
 r2GR
 2G

我很困惑,因为根据循环内使用的三元操作符,如果第6个值是小写字母,它应该将其转换为大写字母。这里'e'是小写的,但输出仍然是小写的,它只是在循环的第一次运行中打印前6个字符。然后在第二次运行中,N递减,从2开始打印。请解释

看起来像这样:

Txt[N] = (isupper(Txt[N] ? tolower(Txt[N]):toupper(Txt[N])));

Txt[N]设置为零,有效地截断了那里的字符串。backkets的配置看起来很可疑——我想知道您的意思是否类似:

Txt[N] = (isupper(Txt[N]) ? tolower(Txt[N]) : toupper(Txt[N]));