美文网首页geeksforgeeks
geeksforgeeks-Array-Reverse a St

geeksforgeeks-Array-Reverse a St

作者: AlanMa | 来源:发表于2017-08-16 10:21 被阅读22次

    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 <iostream>
    #include <cstring>
    using namespace std;
    
    int main() {
        //code
        //get the number of op times
        int T;
        cin>>T;
        
        for(int i=0; i<T; i++)
        {
            //get the string
            char str[30];
            cin.getline(str, 30);
            int len = strlen(str);//get the str length
            cout<<len;
            int end = len - 1;
            int start = 0;
            char tmp;
            while(start<end)
            {
                tmp = str[start];
                str[start] = str[end];
                str[end] = tmp;
                
                start += 1;
                end -= 1;
            }
            
            for(int j=0; j<len; j++)
            {
                cout<<str[j];
            }
            //cout<<endl;
            
        }
        return 0;
    }
    

    第二版
    注意:

    • cin.ignore(100, '\n');忽略缓冲区的前100个字符或'\n',一般把第一个参数设的大一些,为了忽略换行符。例如输入:
      3
      Geeks
      GeeksforGeeks
      GeeksQuiz
      实际上在读Geeks时会读入缓冲区的3的后面的换行符,造成读Geeks变成了读了一个换行符就终止了,造成程序出错。
    • #include <bits/stdc++.h>
      意思是指包含全部C++标准库文件,但是可能存在IDE不支持的情况
    • char字符数组使用strlen获取长度,string类型字符串使用length()函数获取长度
    #include <bits/stdc++.h>
    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<len/2; i++)
            {
                swap(str[i],str[len-i-1]);
            }
            /*
            for(int j=0; j<len; j++)
            {
                cout<<str[j];
            }
            */
            cout<<str<<endl;
            
        }
        return 0;
    }
    

    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 St

        本文链接:https://www.haomeiwen.com/subject/kqsbrxtx.html