输入数据有多组,每组占一行,包含一个句子(句子长度小于1000个字符)
对于每个测试示例,要求输出句子中单词反转后形成的句子
hello xiao mi
mi xiao hello
import java.util.Scanner; public class StringUtil { //句子反转 public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()){ String str = in.nextLine(); String arr[] = str.split(" "); StringBuffer res = new StringBuffer(); for(int i=arr.length-1; i>0; i--){ res.append(arr[i]+" "); } res.append(arr[0]); System.out.println(res.toString()); } } }
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string words; while (getline(cin, words)) { reverse(words.begin(), words.end()); unsigned i = 0, j = i; while (i < words.size()) { while (i < words.size() && words[i] != ' ') ++i; reverse(words.begin() + j, words.begin() + i); j = ++i; } cout << words << endl; } return 0; }
/** *1.先将整个字符串转成字符数组反转得到 mi xiao hello *2.遍历这个字符数组,遇到空格或者到了尾部再做局部反转 */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); char[] arr = s.toCharArray(); for (int i=0; i<arr.length/2; i++) { // 整体反转 char temp = arr[i]; arr[i] = arr[arr.length-1-i]; arr[arr.length-1-i] = temp; } // System.out.println(new String(arr)); int start = 0, end = 0; for (int i=0; i<arr.length; i++) { // 局部反转 if (arr[i]==' ' || i+1==arr.length) { if (arr[i]==' ') end = i-1; else if (i+1==arr.length) end = i; for (int j=start; j<(start+end+1)/2; j++) { char temp = arr[j]; arr[j] = arr[start+end-j]; arr[start+end-j] = temp; } start = i + 1; } } System.out.println(new String(arr)); }
//首先将输入字符串整体逆序,然后再分别逆序每个单词,单词以空格为边界。 //需要注意的是最后一个单词也要逆序,可以最后单独处理。 #include<iostream> #include<string> using namespace std; void Reverse(string &str, int start, int end) //字符串整体逆序 { while (start < end) { swap(str[start], str[end]); start++; end--; } } int main() { string str; while (getline(cin,str)) { int len = str.length(); if (len <= 1) { cout << str << endl; continue; } Reverse(str,0,len-1); //字符串整体逆序,比如输入"hello xiao mi",逆序后变为"im oaix olleh" int index = 0; //记录每个单词第一个字母的位置index,初始为0 for (int i = 0; i < len; i++) { if (str[i] == ' ') //找到第一个空格 { Reverse(str, index, i - 1); //将str中index到i-1位置之间的单词逆序,此时im变为mi index = i + 1; //记录下一个待逆序单词的起始位置 } } Reverse(str, index, len-1); //处理最后一个单词 cout << str << endl; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ String str = scanner.nextLine(); String[] strs = str.split(" "); for (int i = strs.length-1; i >= 0; i--) { System.out.print(strs[i]); if(i != 0){ System.out.print(" "); } } } } }
#include <iostream> #include <string> #include <vector> using namespace std; int main() { string str1, temp; vector<string> v1; getline(cin, str1); for (int i = 0;i < str1.size();++i) { if (str1[i] != ' ') temp += str1[i]; else { if (!temp.empty()) { v1.push_back(temp); temp.clear(); } } } if (!temp.empty()) v1.push_back(temp); int i; for (i = v1.size() - 1;i > 0;--i) cout << v1[i] << ' '; cout << v1[i] << endl; return 0; }
#include<stdio.h> #include<string.h> int main() { int n,i=0,j,k; char s[1001],t[1001],c,temp; for(i=0;(c=getchar())!='\n';) { s[i++]=c; } s[i]='\0'; i--; for(;i>=0;)//从句子尾部开始,遇到空格则抽离一个单词,得到的单词是倒序的,再通过一次反转 { for(j=0;(s[i]!=' ')&&(i>=0);j++,i--) t[j]=s[i]; t[j]='\0'; j--; for(k=0;k<j;k++,j--) { temp=t[j]; t[j]=t[k]; t[k]=temp; } printf("%s",t); if(i<0) break; else { i--; printf(" ");} } printf("\n"); return 0; }
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
String[] nextLine = new Scanner(System.in).nextLine().split(" ");
StringBuilder stringBuilder = new StringBuilder();
for (int i = nextLine.length-1; i >= 0; i--) {
if (i==0) {
stringBuilder.append(nextLine[i]);
}else{
stringBuilder.append(nextLine[i]+" ");
}
}
System.out.println(stringBuilder);
}
}
#include<iostream> #include <string> #include<vector> using namespace std; int main() { vector<string>a; string temp; char b; while((cin>>temp).get(b)){ a.push_back(temp); if(b=='\n') break; } for(int i = 0;i<a.size();i++){ cout<<a[a.size()-1-i]; if(i!=a.size()-1) cout<<" "; } system("pause"); return 0; }
#include<iostream> #include<string> #include<sstream> using namespace std; int main() { string line, result, word; while (getline(cin, line)) { istringstream ism(line); ism >> result; while (ism >> word) result = word + " " + result; cout << result; } return 0; }