Xinao Sai Yi Yi Tong 1138: Convert lowercase letters in a string to uppercase letters

【Description】

Given a string, convert all lowercase letters to uppercase.

【enter】

Enter a line, containing a string (length not exceeding 100, may contain spaces).

【Output】

Output the converted string.

【Input example】

helloworld123Ha

【Example of output】

HELLOWORLD123HA

C language:
#include<stdio.h>
#include<string.h>
int main()
{
    char s[10001];                         //定义一个字符类型的数组
    gets(s);                               //用gets输入字符串
    int length=strlen(s);                  //计算字符串的长度
    for(int i=0;i<length;i++)             //遍历每个字符
    {
        if((s[i]>='a')&&(s[i]<='z'))     //判断该字符是否为小写字母
        {
            s[i]-=32;                     //若是,则转换为大写字母
        }
        printf("%c",s[i]);              //输出该字符(可能呗转换也可能没被)
    }
    return 0;
}
C++:
#include<iostream>
using namespace std;
int main()
{
    string s;                   //使用C++中的string类来定义
    getline(cin,s);             //输入字符串

    for(int i=0;i<s.size();i++)    //遍历字符串
    {
        if(('a'<=s[i])&&(s[i]<='z'))      //判断是否为小写字母
        {
            s[i]-=32;                      //若是,则转换为大写字母
        }
        cout<<s[i];                        //输出
    }
    cout<<endl;                       //换行(可有可无)
    return 0;
}

For case conversion, just remember: the conversion from lower case to upper case is the decimal ASCII code minus 32 , and vice versa, the conversion from upper case to lower case is plus 32 .

Guess you like

Origin blog.csdn.net/H1727548/article/details/129192094