接受一个只包含小写字母的字符串,然后输出该字符串反转后的字符串。(字符串长度不超过1000)
#include <bits/stdc++.h> using namespace std; int main(){ string s; cin >> s; string ans; reverse(s.begin(), s.end()); ans = s; cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ string s; cin >> s; int left = 0, right = s.size() - 1; while(left < right){ swap(s[left], s[right]); left++; right--; } cout << s; return 0; }
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc=new Scanner(System.in); StringBuffer str=new StringBuffer(sc.nextLine()); System.out.print(str.reverse()); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); String s = input.nextLine(); StringBuilder sb = new StringBuilder(); for(int i=s.length()-1; i>=0; i--){ sb.append(s.charAt(i)); } System.out.print(sb); } }法二:
import java.util.*; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); StringBuilder sb = new StringBuilder(); while(input.hasNext()){ sb.append(input.next()); } System.out.print(sb.reverse()); } }
#include<stdio.h> #include<string.h> int main() { char a[1000] = {0}; while (gets(a)) { char buf[1000] = {0}; unsigned long len = strlen(a); for (long i = 0; i < len; i++) { buf[len - i - 1] = a[i]; } printf("%s", buf); } return 0; }
import java.util.Scanner; public class Main{ public static void main(String[] args){ String str = new Scanner(System.in).next(); StringBuffer strR = new StringBuffer(str).reverse(); System.out.println(strR); } }
#include <stdio.h> (737)#include <string.h> #include <stdlib.h> (794)#include <math.h> int strlen_count(char* str); // 计算字符串的长度,包括‘\0’ // 但不包括‘\n’换行符 int main(void) { char str[1000] = { 0 }; int str_len = 0; fgets(str, 1000, stdin); str_len = strlen_count(str); for (int i = str_len; i > 0; i--) { printf("%c", str[i - 1]); } printf("\r\n"); system("pause"); return 0; } int strlen_count(char* str) { int count = 0; while (*(str) != '\n') { str++; // 指向下一个字符 count++; } return count; }
while(True): # 方法1:转换为list进行操作 try: s = input() s = list(reversed(list(s))) # reversed返回的是迭代器,所以要加list() print(''.join(s)) except: break ''' while(True): # 方法2:直接对字符串s进行reversed 操作 try: s = input() print(''.join(reversed(s))) except: break ''' while(True): # 采用字符串切片的方式 try: s = input() print(s[::-1]) except: break
//1.一般算法 #include <stdio.h> int main() { char a[500]="\0"; int i; gets(a); for(i=499;i>=0;i--) if(a[i]!='\0') printf("%c",a[i]); return 0; } //2.递归算法 #include <stdio.h> void hahaha() { char c; if(scanf("%c",&c)!=EOF) { if(c!='\n') { hahaha(); printf("%c",c); } } } int main() { hahaha(); return 0; }
#include<iostream> #include<cstring> using namespace std; int main(){ char a[1000]; cin>>a; int len=strlen(a); for(int i=len-1;i>=0;i--) cout<<a[i]; cout<<endl; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); String s = in.nextLine(); String[] ss = s.split(""); String result = ""; for(int i=0;i<ss.length;i++){ result += ss[ss.length-1-i]; } System.out.println(result); } }