我一直在试图弄清楚如何到达以下程序的输出。我基本上想知道每次迭代后字符数组的状态。会有很大帮助。
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char *name="ThE bESt meN wIN";
for(int i=0;name[i]!='\0';i++)
{
if(islower(name[i]))
name[i]=toupper(name[i]);
else
if(isupper(name[i]))
{
if(i%2==0)
name[i]--;
else
name[i]=tolower(name[i-1]);
}
}
cout<<name;
getch();
}
输出是: SHD BbRT MEe WHh
答案 0 :(得分:0)
将main
返回类型更改为int
并修改此行,使其指向可修改的字符数组:
char name[]="ThE bESt meN wIN";
然后一步一步地进行,这里是16次迭代中每次迭代的输出。数字指的是实际的数组索引。这应该会有所帮助。
如果不使用历史
,也许这更容易处理initial
ThE bESt meN wIN
0123456789012345
ShE bESt meN wIN 0: T -> S
0123456789012345
SHE bESt meN wIN 1: h -> H
0123456789012345
SHD bESt meN wIN 2: E -> D
0123456789012345
SHD bESt meN wIN 3: space
0123456789012345
SHD BESt meN wIN 4: b -> B
0123456789012345
SHD BbSt meN wIN 5: E -> b
0123456789012345
SHD BbRt meN wIN 6: S -> R
0123456789012345
SHD BbRT meN wIN 7: t -> T
0123456789012345
SHD BbRT meN wIN 8: space
0123456789012345
SHD BbRT MeN wIN 9: m -> M
0123456789012345
SHD BbRT MEN wIN 10: e -> E
0123456789012345
SHD BbRT MEe wIN 11: N -> e
0123456789012345
SHD BbRT MEe wIN 12: space
0123456789012345
SHD BbRT MEe WIN 13: w -> W
0123456789012345
SHD BbRT MEe WHN 14: I -> H
0123456789012345
SHD BbRT MEe WHh 15: N -> h