首页 > 试题广场 >

字符串的反码

[编程题]字符串的反码
  • 热度指数:6218 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    一个二进制数,将其每一位取反,称之为这个数的反码。下面我们定义一个字符的反码。如果这是一个小写字符,则它和字符'a’的距离与它的反码和字符'z’的距离相同;如果是一个大写字符,则它和字符'A’的距离与它的反码和字符'Z’的距离相同;如果不是上面两种情况,它的反码就是它自身。     举几个例子,'a’的反码是'z’;'c’的反码是'x’;'W’的反码是'D’;'1’的反码还是'1’;'$'的反码还是'$'。     一个字符串的反码定义为其所有字符的反码。我们的任务就是计算出给定字符串的反码。

输入描述:
    输入是一个字符串,字符串长度不超过 80 个字符。


输出描述:
输出其反码
示例1

输入

Hello

输出

Svool
示例2

输入

JLU-CCST-2011 

输出

QOF-XXHG-2011
#include<iostream>
using namespace std;
int main()
{
	string s;
	cin >> s;
	for(int i=0; i<s.length(); i++)
	{
		if( s[i]>='a'&&s[i]<='z'  )
		{
			s[i] = 'z' + 'a' - s[i];
		}
		if( s[i]>='A'&&s[i]<='Z' )
		{
			s[i] = 'Z' + 'A' - s[i];
		}
	}
	cout << s << endl;
}

发表于 2021-02-26 00:28:50 回复(0)

python solution:


while True:
    try:
        string=input()
        if string!="!":
            res=""
            for i in string:
                if i.isupper():
                    res+=chr(ord("Z")-(ord(i)-ord("A")))
                elif i.islower():
                    res += chr(ord("z") - (ord(i) - ord("a")))
                else:
                    res+=i
            print(res)
    except:
        break
发表于 2017-10-16 18:33:37 回复(0)
#include<iostream>
using namespace std;
int main() {
    string str;
    int num;
    while(cin>>str&&str!="!") {
        string ans="";
        for(int i=0; i<str.size(); i++) {
            if(str[i]-'A'>=0&&str[i]-'A'<=25) {
                num=str[i]-'A';
                ans+='Z'-num;
            }
            else if(str[i]-'a'>=0&&str[i]-'a'<=25) {
                num=str[i]-'a';
                ans+='z'-num;
            } else {
                ans+=str[i];
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
发表于 2020-04-20 12:34:01 回复(0)
#include<stdio.h>
#include<string.h>
int main()
{
    char a[100];
    int n,i;
    while(gets(a))
    {
        if(a[0]=='!'&&a[1]=='\0') break;
        n=strlen(a);
        for(i=0;i<n;i++)
        {
            if(a[i]>='a'&&a[i]<='z')
                a[i]='z'-(a[i]-'a');
            if(a[i]>='A'&&a[i]<='Z')
                a[i]='Z'-(a[i]-'A');
        }
        printf("%s\n",a);
    }
}

编辑于 2020-03-26 21:47:49 回复(0)
Java 解法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String s = scanner.next();
            if (s.equals("!")){
                break;
            }else {
                StringBuilder builder = new StringBuilder();
                char[] array = s.toCharArray();
                for (char c : array) {
                    if (c >= 'a' && c <= 'z')
                        builder.append((char) ('z' - (c - 'a')));
                    else if (c>='A'&&c<='Z')
                        builder.append((char) ('Z' - (c - 'A')));
                    else 
                        builder.append(c);
                }
                System.out.println(builder.toString());
            }
        }
    }
}


发表于 2020-03-08 22:28:28 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main(){
    char ch[81];
    while(cin>>ch){
        if(ch[0]=='!')
            break;
        for(int i=0;i<strlen(ch);i++){
            if(ch[i]>='a'&&ch[i]<='z')
                cout<<(char)('a'+(25-(ch[i]-'a')));
            else if(ch[i]>='A'&&ch[i]<='Z')
                cout<<(char)('A'+(25-(ch[i]-'A')));
            else
                cout<<ch[i];
        }
        cout<<endl;
    }
}

发表于 2020-01-13 20:37:03 回复(0)
#include<bits/stdc++.h>
int main(){
    char a[81];
    while(gets(a)){
        if(a[0]=='!'&&a[1]=='\0') break;
        for(int i=0;i<strlen(a);i++){
            if(a[i]>='a'&&a[i]<='z')
                a[i]='z'-a[i]+'a';
            else if(a[i]>='A'&&a[i]<='Z')
                a[i]='Z'-a[i]+'A';
            printf("%c",a[i]);
        }
        printf("\n");
    }
}
发表于 2019-03-20 09:07:17 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    while(getline(cin,str))
    {
        if(str=="!")
            break;
        else
        {
            for(int i=0;i<str.length();i++)
            {
                if(str[i]>='a' && str[i]<='z')
                    printf("%c",(int)('z'-(str[i]-'a')));
                 else if(str[i]>='A' && str[i]<='Z')
                    printf("%c",(int)('Z'-(str[i]-'A')));
                 else
                    printf("%c",str[i]);
                         
            }
                 printf("\n");
        }
    }
}


发表于 2019-03-01 20:00:50 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        StringBuilder sb = new StringBuilder("");
        while(sc.hasNext()){
            String str = sc.nextLine();
            if('!' == str.charAt(0)){
                break;
            }
            char[] ch = str.toCharArray();
            for(int i=0;i<ch.length;i++){
                sb.append(rev(ch[i]));
            }
            System.out.print(sb);
        }
    }
    public static char rev(char c){
        if(c>='a' && c<='z'){
            return (char)('z'+'a'-c);
        }else if(c>='A' && c<='Z'){
            return (char)('A'+'Z'-c);
        }
        return c;
    }
}

发表于 2018-08-03 06:39:02 回复(0)
#include <stdio.h>

int main() {
    char s[80];
    scanf("%s",s);
    for(int i=0;s[i]!='\0';i++){
        switch(s[i]){
            case 'a':s[i]='z';break;
            case 'b':s[i]='y';break;
            case 'c':s[i]='x';break;
            case 'd':s[i]='w';break;
            case 'e':s[i]='v';break;
            case 'f':s[i]='u';break;
            case 'g':s[i]='t';break;
            case 'h':s[i]='s';break;
            case 'i':s[i]='r';break;
            case 'j':s[i]='q';break;
            case 'k':s[i]='p';break;
            case 'l':s[i]='o';break;
            case 'm':s[i]='n';break;
            case 'n':s[i]='m';break;
            case 'o':s[i]='l';break;
            case 'p':s[i]='k';break;
            case 'q':s[i]='j';break;
            case 'r':s[i]='i';break;
            case 's':s[i]='h';break;
            case 't':s[i]='g';break;
            case 'u':s[i]='f';break;
            case 'v':s[i]='e';break;
            case 'w':s[i]='d';break;
            case 'x':s[i]='c';break;
            case 'y':s[i]='b';break;
            case 'z':s[i]='a';break;
            case 'A':s[i]='Z';break;
            case 'B':s[i]='Y';break;
            case 'C':s[i]='X';break;
            case 'D':s[i]='W';break;
            case 'E':s[i]='V';break;
            case 'F':s[i]='U';break;
            case 'G':s[i]='T';break;
            case 'H':s[i]='S';break;
            case 'I':s[i]='R';break;
            case 'J':s[i]='Q';break;
            case 'K':s[i]='P';break;
            case 'L':s[i]='O';break;
            case 'M':s[i]='N';break;
            case 'N':s[i]='M';break;
            case 'O':s[i]='L';break;
            case 'P':s[i]='K';break;
            case 'Q':s[i]='J';break;
            case 'R':s[i]='I';break;
            case 'S':s[i]='H';break;
            case 'T':s[i]='G';break;
            case 'U':s[i]='F';break;
            case 'V':s[i]='E';break;
            case 'W':s[i]='D';break;
            case 'X':s[i]='C';break;
            case 'Y':s[i]='B';break;
            case 'Z':s[i]='A';break;
        }
    }
    printf("%s",s);
}

发表于 今天 09:50:09 回复(0)
def revword(s):
    res = []
    for i in range(len(s)):
        if ord('A') <= ord(s[i]) and ord(s[i]) <= ord('Z'):
              a = ord(s[i]) - ord('A')
              r = ord('Z') - a
              res.append(chr(r))
        elif ord('a') <= ord(s[i]) and ord(s[i]) <= ord('z'):
              a = ord(s[i]) - ord('a')
              r = ord('z') - a
              res.append(chr(r))
        # elif ord('a') == ord(s[i]):
        #      res.append('z')
        # elif ord('z') == ord(s[i]):
        #      res.append('a')
        else:
             res.append(s[i])
    return ''.join(res)
s = input()
print(revword(s))

发表于 2025-02-26 22:46:39 回复(0)
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    while (cin >> str) {
        for (auto& ch : str) {
            if (ch >= 'A' && ch <= 'Z') {
                ch -= 'A';
                ch = 25 - ch;
                ch += 'A';
            } else if (ch >= 'a' && ch <= 'z') {
                ch -= 'a';
                ch = 25 - ch;
                ch += 'a';
            }
        }
        cout << str << endl;
    }
    return 0;
}

编辑于 2024-03-04 10:08:20 回复(0)
#include <iostream>
using namespace std;

int main()
{
    string a;
    cin >> a;
    for(int i = 0; a[i]; i++)
    {
        if(a[i] >= 'a' && a[i] <= 'z') a[i] = 25 - (a[i] - 'a') + 'a';
        if(a[i] >= 'A' && a[i] <= 'Z') a[i] = 25 - (a[i] - 'A') + 'A';
    }
    cout << a << endl;
    return 0;
}

发表于 2023-03-28 19:43:10 回复(0)
using namespace std;
char rev(char x)
{
    if('a'<=x&&x<='z')
    {
        x='z'-(x-'a');
        return x;
    }
    else if('A'<=x&&x<='Z')
    {
        x='Z'-(x-'A');
        return x;
    }
    else {
    return x;
    }
}
int main() {
   string str;
    while (cin>>str) {
        for(int i=0;i<str.size();i++)
        {
            str[i]=rev(str[i]);
        }
        cout<<str<<endl;
    }
}
发表于 2023-03-27 21:40:15 回复(0)
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main(){
    string str;
    while(cin>>str){
        for(int i = 0; i < str.size(); ++i){
            if( ('a'<=str[i] && str[i]<='z') || ('A'<=str[i] && str[i]<='Z')){
                int x; //偏移量
                if('a'<=str[i] && str[i]<='m'){
                    x = str[i]-'a';
                    str[i] = 'z'-x;
                }else if('n'<=str[i] && str[i]<='z'){
                    x = 'z'-str[i];
                    str[i] = 'a'+x;
                }else if('A'<=str[i] && str[i]<='M'){
                    x = str[i]-'A';
                    str[i] = 'Z'-x;
                }else if('N'<=str[i] && str[i]<='Z'){
                    x = 'Z'-str[i];
                    str[i] = 'A'+x;
                }
            }
        }
        cout<<str<<endl;
    }

    return 0;
}

发表于 2023-03-25 21:42:56 回复(0)
#include <cctype>
#include <iostream>
using namespace std;
string fan(string s){
    for(int i=0;i<s.size();i++){//遍历字符串
        if(islower(s[i])){//如果是小写字母
            s[i]=(25-(int)(s[i]-'a'))+'a';
        }
        if(isupper(s[i])){//如果是大写字母
            s[i]=(25-(int)(s[i]-'A'))+'A';
        }
    }
    return s;
}
int main() {
    string s;
    while(cin>>s){
        cout<<fan(s)<<endl;
    }
}

发表于 2023-03-18 10:25:18 回复(0)
#include <iostream>
using namespace std;

int main() {

    char str[81];
    while (cin >> str) {
        for (int i = 0 ; str[i] != '\0' ; i++) {
            if (str[i] >= 'A' && str[i] <= 'Z') {
                str[i] = 'Z' - (str[i] - 'A');
            }
            if (str[i] >= 'a' && str[i] <= 'z') {
                str[i] = 'z' - (str[i] - 'a');
            }
        }
        for (int i = 0 ; str[i] != '\0' ; i++) {
            cout << str[i];
        }
        cout << endl;
    }
}

发表于 2022-08-20 20:52:08 回复(0)
#include<iostream>
using namespace std;

int isAlpha(char x)
{
    if(x>='a'&&x<='z') return 1;
    if(x>='A'&&x<='Z') return 2;
    return 0;
}

int main()
{
    string str;
    while(cin>>str)
    {
        if(str=="!") break;
        for(int i=0;i<str.size();i++)
        {
            int temp=isAlpha(str[i]);
            if(temp==1)
                str[i]='z'-(str[i]-'a');
            if(temp==2)
                str[i]='Z'-(str[i]-'A');
        }
        cout<<str<<endl;
    }
}

发表于 2022-03-04 00:00:19 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = br.readLine()) != null) {
            if (s.equals("!")) break;
            StringBuilder re = new StringBuilder("");
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c >= 'a' && c <= 'z') {
                    re.append(fun(c));
                } else if (c >= 'A' && c <= 'Z') {
                    re.append(Fun(c));
                } else {
                    re.append(c);
                }
            }
            System.out.println(re);
        }

    }

    private static char fun(char c) {
        int t = c - 'a';
        return (char) ('z' - t);
    }

    private static char Fun(char c) {
        int t = c - 'A';
        return (char) ('Z' - t);
    }


}


发表于 2021-04-13 20:58:18 回复(0)
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    while(cin >> str && str!="!")
    {
        for(int i=0;i<str.size();i++)
        {
            if(str[i]>='A'&&str[i]<='Z')
            {
                str[i]='Z'-(str[i]-'A');
            }
            else if(str[i]>='a'&&str[i]<='z')
            {
                str[i]='z'-(str[i]-'a');
            }
            cout << str[i];
        }
        cout << endl;
    }
    return 0;
}

发表于 2021-03-29 20:47:38 回复(0)