读书人

请大师帮小弟我解析上马下要考试了

发布时间: 2013-01-12 16:25:03 作者: rapoo

请大师帮我解析下,马上要考试了,哎。。。
#include <iostream>
using namespace std;
int main()
{
char a[] = "Hello, World";
char *ptr = a;
while (*ptr)
{
if (*ptr >= 'a' && *ptr <= 'z')
cout << char(*ptr + 'A' -'a');
else cout << *ptr;
ptr++;
}
return 0;
}
A ) HELLO, WORLD B ) Hello, World
C ) hELLO, wORLD D ) hello, world

[解决办法]
选A啊
char类型对应ascii码

#include <iostream>
using namespace std;
int main()
{
char a[] = "Hello, World";
char *ptr = a;//指向a[0],++之后指向a[1],再++ 指向a[2]。。。。。
while (*ptr)//遍历数组
{
if (*ptr >= 'a' && *ptr <= 'z')//元素在'a'和'z'之间,即小写字母
cout << char(*ptr + 'A' -'a');//输出转换为大写字母
else cout << *ptr;
ptr++;
}
return 0;
}

读书人网 >C++

热点推荐