首页 > 试题广场 >

字母转换

[问答题]

题目标题:

字母转换

题目描述:

输入一个字符串(长度<100),将其中所有大写字母改为小写字母,而所有小写字母全部改为大写字母,其余字符原样输出然后输出前n个字符。 设计步骤 ①输入字符串; ②用循环判断字符串中的每个字符是大写还是小写,若是大写要转换成小写,是小写要转换成大写:大写与小写字母的转换关系是“小写字母=大写字母+32” ③用printf输出该字符串前n个字符。

输入描述:

输入数据为1个数字,表示输出字符窜长度(n<100),接着输入1个字符串,其长度不限,可包含键盘上可见的所有字符

输出描述:

输出为1个字符串,根据输入数据,将其中的大写字符转换成小写,小写转换成大写,其余字符不变

样式输入:

5

FG56hj

样式输出:

fg56H

#include <iostream>
#include <string>
using namespace std;
int main(){
    int n;
    string str;
    getline(cin,str);
    for(int i=0;i<n;i++){
        if(str[i]>='A' && str[i]<='Z'){
            str[i]=str[i]-'A'+'a';
        }
        else if(str[i]>='a' && str[i]<='z'){
            str[i]=str[i]-'a'+'A';
        }
    }
    cout<<str<<endl;
    return 0;
}

发表于 2023-09-13 11:13:52 回复(0)
function convert(input, length) {
    const arr = [...input]
    let charCodeA = 'A'.charCodeAt()
    let charCodeB = 'Z'.charCodeAt()
    let charCodeC = 'a'.charCodeAt()
    let charCodeD = 'z'.charCodeAt()
    let result =''
    let index = 0
    while(arr.length > 0) {
        if(index >= length) return result
        let ch = arr.shift()
        let charCode = ch.charCodeAt()
        if(charCode>= charCodeA && charCode <= charCodeB) {
            charCode += 32
        } else if(charCode >= charCodeC && charCode <= charCodeD) {
            charCode -=32
        }
        
        let newStr = String.fromCharCode(charCode)
        result = result + newStr
        index++
    }
    return result
}

发表于 2024-02-25 17:32:05 回复(0)
public static void main(String[] args) {
    Scanner in =new Scanner(System.in); int number = Integer.parseInt(in.nextLine()); char [] str =in.nextLine().toCharArray(); for (int i=0 ;i<number&&i<str.length;i++){ if(str[i]>=97 && str[i]<=122) {
            System.out.print((char) (str[i] - 32));
        }else if (str[i]>=65 && str[i]<=90) {
            System.out.print((char)(str[i]+32));
        }else{
            System.out.print(str[i]);
        }
    }
}
发表于 2023-05-11 11:31:21 回复(0)
public static void changeChars() {
        try (Scanner sc = new Scanner(System.in)) {
            int length = 0;
            length = sc.nextInt();
            if (length <= 0) {
                System.out.println("error");
            }
            String waitChangeString = sc.next();
            char[] waitChangeChars = waitChangeString.toCharArray();

            for (int i = 0; i < length && i < waitChangeChars.length; i++) {
                char temp = waitChangeChars[i];
                if (temp >= 97 && temp <= 122) {
                    temp -= 32;
                } else if (temp >= 65 && temp <= 90) {
                    temp += 32;
                }
                System.out.print(temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

发表于 2023-04-28 18:38:18 回复(0)
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //输入的数字
        int count = scanner.nextInt();
        //换行
        scanner.nextLine();
        //输入的字符串
        String s = scanner.nextLine();
        //输入的字符串数组
        char[] original = s.toCharArray();

        //数组长度
        int length = original.length;

        //目标数组
        char[] target = new char[length];

        for (int i = 0; i < length; i++) {

            //大写字母转换为小写字母
            if(original[i] >= 65 && original[i] <= 91) {
                target[i] = (char) (original[i] + 32);
            }
            // 小写字母转换为大写字母
            else if(original[i] >= 97 && original[i] <= 123) {
                target[i] = (char) (original[i] - 32);
            }
            //其他字符
            else {
                target[i] = original[i];
            }
        }
        //转化为字符串
        String targetStr = new String(target);
        //截取前count个字符串输出
        System.out.println(targetStr.substring(0,count));
    }

编辑于 2023-02-27 22:38:30 回复(0)
var str = n.toString()
var strArray = str.split('')
var newStr = ''
strArray.map(i => {
    var strCode = i.charCodeAt();
    var strChange = ''
    if(strCode >= 65 && strCode <= 90){
    // 你输入的是英文大写字母
      strChange = i.toLowerCase() // 转换成小写 
      newStr += strChange
    }else if(strCode >= 97 && strCode <= 122){
    // 你输入的是英文小写字母
       strChange = i.toUpperCase() // 转换成大写 
       newStr += strChange
    }else{
        // 你输入的是其他符号
        newStr += i
    }
})
return newStr


编辑于 2023-02-02 09:37:49 回复(0)

#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
int n;
scanf("%d",&n);
getchar();
char a[105];
gets(a);
int i,l=strlen(a);
for(i=0;i<l;i++)
{
if(a[i]>='A'&&a[i]<='Z')
a[i]+=32;
else if(a[i]>='a'&&a[i]<='z')
a[i]-=32;
}
for(i=0;i<n;i++)
printf("%c",a[i]);
return 0;
}

发表于 2017-05-15 00:19:27 回复(0)