geeksforgeeks-Array-Reverse a String

Given a string S as input. You have to reverse the given string.

Input: First line of input contains a single integer T which denotes the number of test cases. T test cases follows, first line of each test case contains a string S.

Output: Corresponding to each test case, print the string S in reverse order.

Constraints:

1<=T<=100
3<= length(S) <=1000

Example:

Input:
3
Geeks
GeeksforGeeks
GeeksQuiz

Output:
skeeG
skeeGrofskeeG
ziuQskeeG

C++代码

#include 
#include 
using namespace std;

int main() {
    //code
    //get the number of op times
    int T;
    cin>>T;
    
    for(int i=0; i

第二版
注意:

  • cin.ignore(100, '\n');忽略缓冲区的前100个字符或'\n',一般把第一个参数设的大一些,为了忽略换行符。例如输入:
    3
    Geeks
    GeeksforGeeks
    GeeksQuiz
    实际上在读Geeks时会读入缓冲区的3的后面的换行符,造成读Geeks变成了读了一个换行符就终止了,造成程序出错。
  • #include
    意思是指包含全部C++标准库文件,但是可能存在IDE不支持的情况
  • char字符数组使用strlen获取长度,string类型字符串使用length()函数获取长度
#include 
using namespace std;

int main() {
    //code
    //get the number of op times
    int T;
    cin>>T;
    //advoid read the '\n'
    cin.ignore(100, '\n');  
    while(T--)
    {
        //get the string
        //char str[30];
        //cin.getline(str, 30);
        ////or
        string str;
        getline(cin,str);
        int len = str.length();//get the str length
        
        //int len = strlen(str);//get the str length
        for(int i=0; i

python代码

#code
def reverse_string(str):
    reverse_str = str[::-1]
    
    return reverse_str
    

# get the number of op times
T = int(input())

for i in range(0,T):
    # get the string
    str = input()
    reverse_str = reverse_string(str)

    # print string
    print(reverse_str)

你可能感兴趣的:(geeksforgeeks-Array-Reverse a String)