这个不明白啥意思请解释一下

空题
void main( )

{ char str[]="Hello!"; char *p=str;

while(*p)

{ if( *p>=‘a’&& *p<=‘z’)

cout<p +‘A’-‘a’);

p++;}

}
程序的运行结果为:

这就是小写字符转大写字符啊。输出HELLO!

会报错:
1.cout<应该改为cout<<
2.p +‘A’-‘a’)的有括号没匹配(
3.‘a’是中文引号
改了后运行结果是啥也没有:

img

首先,看题目和其他人的理解,你的目的大概率是将字符串中的小写字母转换为大写字母,
大写字母的值A-Z 是65-90 小写字母a-z 是97-122。
这个小程序的基础原理,就是利用大写字母和小写字母之间的差值是固定的来计算实现了。
当然也可以判断是不是数字之类的,这里只考虑大写字母和小写字母就好了。
修改代码如下所示:

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    char str[]="Hello";
    char *p = str;
    printf("A-%d Z=%d a=%d,z=%d\n",'A','Z','a','z');
    while(*p){
        if(*p >= 'a' && *p <= 'z')
            cout << (char)(*p + 'A' - 'a');
        else{
            cout << (char)*p;
        }
        p++;
    }
    cout << endl;
    return 0;
}


编译并执行:

csdn@ubuntu:~$ g++ test.cpp
csdn@ubuntu:~$ ./a.out
A-65 Z=90 a=97,z=122
HELLO
csdn@ubuntu:~$


C语言版本:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
    char str[]="Hello";
    char *p = str;
    printf("A-%d Z=%d a=%d,z=%d\n",'A','Z','a','z');
    while(*p){
        if(*p >= 'a' && *p <= 'z')
            printf("%c",*p + 'A' - 'a');
        else{
            printf("%c",*p);
        }
        p++;
    }
    printf("\n");
    return 0;
}


编译并执行:

csdn@ubuntu:~$ ./a.out
A-65 Z=90 a=97,z=122
HELLO
csdn@ubuntu:~$