首页 > 试题广场 >

编程基础

[编程题]编程基础
  • 热度指数:370 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个非空字符串,输出该字符串的逆序字符串。字符串的长度不超过20个字符

输入描述:
字符串


输出描述:
转换后的逆序字符串
超过20个字符时的打印
示例1

输入

I like coding

输出

gnidoc ekil I
示例2

输入

I want to restart 2020

输出

The string you entered has exceeded the limit

说明

您输入的字符串已超出限制
def rev(s):
    if len(s)>20:
        print("The string you entered has exceeded the limit")
    else:
        print(s[::-1])
s = input()  # 获取字符串数据,input()默认格式是字符串
rev(s)  # 执行函数

发表于 2022-02-28 15:45:08 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str=scanner.nextLine();
        String str2="";
        if(str.length()>20){
            System.out.print("The string you entered has exceeded the limit");
            return;
        }
     for(int i=str.length()-1;i>=0;i--){
        str2+=str.charAt(i);
     }
     System.out.print(str2.toString());
     
    }
}
发表于 2023-08-27 19:41:02 回复(0)
str = input()
if len(str) <= 21:
    s = list(str)
    s.reverse()
    print(''.join(s))
else:
    print('The string you entered has exceeded the limit')


发表于 2022-07-30 18:33:12 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()){
            String  str = sc.nextLine();
            StringBuilder sb = new StringBuilder();
            StringBuilder sb1 = new StringBuilder();
            if(str.length() > 20){
                System.out.println("The string you entered has exceeded the limit");
                break;
            }
            for(int i = 0; i<str.length();i++){
                sb.append(str.charAt(i));
            }
            for(int i = sb.length()-1; i>=0;i--){
                sb1.append(str.charAt(i));
            }
            System.out.println(sb1);
        }
    }
}

发表于 2022-03-21 10:17:58 回复(0)
function rev(str){
        if((str.length!==0)&&(str.length<=20)){
            let arr = str.split('')
            arr.reverse();
            let result=arr.join('')
            console.log(result)
        }else if(str.length>20){
            console.log('The string you entered has exceeded the limit')
            return false;
        }else{
            console.log('字符串不能为空')
        }
    }
rev('abcdefghigklmnopqrstuvmxyz');
发表于 2022-02-22 19:40:37 回复(0)
class Solution:
    def strReverse(self, p):
        a = []
        if len(p) > 20:
            return "The string you entered has exceeded the limit"

        for each in p:
            a.append(each)

        a.reverse()
        b = "".join(a)

        return b

if __name__ == "__main__":
    gifts = input()
    function = Solution()
    results = function.strReverse(gifts)
    print(results)

发表于 2022-02-21 18:04:41 回复(0)