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 <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');忽略緩沖區(qū)的前100個字符或'\n',一般把第一個參數(shù)設(shè)的大一些,為了忽略換行符。例如輸入:
    3
    Geeks
    GeeksforGeeks
    GeeksQuiz
    實(shí)際上在讀Geeks時會讀入緩沖區(qū)的3的后面的換行符,造成讀Geeks變成了讀了一個換行符就終止了,造成程序出錯。
  • #include <bits/stdc++.h>
    意思是指包含全部C++標(biāo)準(zhǔn)庫文件,但是可能存在IDE不支持的情況
  • char字符數(shù)組使用strlen獲取長度,string類型字符串使用length()函數(shù)獲取長度
#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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,936評論 0 33
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,715評論 19 139
  • 今日陽光正好,約了朋友到大學(xué)城騎行。兩人都是40歲左右的年紀(jì),好久沒騎單車,在嶺南印象園到中心湖的路程上感觸頗多。...
    xindong_ying閱讀 939評論 0 0
  • 心若不動,風(fēng)又奈何; 你若不傷,歲月無恙。 輕風(fēng)搖曳,曾聞江楓漁火愁難眠的離人夜。
    時光云廊閱讀 505評論 0 2
  • 你的笑一閃而過 我還沒來得及拍照 不停的回到那一刻 以致于現(xiàn)在記憶錯亂 只剩下一張嘴 在我的腦門上張開 這會讓我瘋...
    齊家能閱讀 164評論 0 0

友情鏈接更多精彩內(nèi)容