将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I” 所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符 数据范围:输入的字符串长度满足 注意本题有多组输入
区块链毕设网qklbishe.com为您提供问题的解答
将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”
#include <stdio.h> #include <string.h> int main() { //字符串str的长度要定为1000,如果太小的话,会溢出 char str[1000]={0}; while (gets(str)) { int len = strlen(str); int left = len-1; //这里,left和right初始化的时候都指向字符串的末尾 int right = len-1; while(left>0) { //left回退到空格的地方 while(str[left]!=' ' && left>0) left--; //如果回退到left为0的话,就要break出这个循环,为的是不要做后面的更新处理 if(left == 0) break; //left每回退到空格的地方,都可以打印出一组字符串 for(int i=left+1; i<=right; i++) printf("%c", str[i]); printf(" "); //更新left和right 的值 right = left-1; left--; } //打印出最后一组值,也就是left为0的那组 for(int i=left; i<=right; i++) printf("%c", str[i]); printf("n"); } return 0; }
}
#include <iostream> #include <string> using namespace std; int main() { string a ,s; while(cin >> a) s = a + " " + s; cout << s << endl; return 0; }
#include <stdio.h> int main(){ char input[100][100]; int count = 0; while (scanf("%s", input[count]) > 0){ count++; } for (int i = 0; i < count; i++){ printf("%s ", input[count - i - 1]); } }
#include<bits/stdc++.h> using namespace std; int main(){ vector<string> a; string b; while(cin>>b){ a.push_back(b); } for(int i=a.size()-1;i>=0;i--){ cout<<a[i]<<" "; } return 0; }
import java.util.Deque; import java.util.LinkedList; import java.util.Scanner; /** * 功能描述: * * @Author: xxx * @Date: 2021/6/28 16:06 */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //使用双端队列 Deque<String> deque = new LinkedList<>(); String testString = scanner.nextLine(); //过滤掉非英文字符 testString = testString.replaceAll("[^a-zA-Z\s]",""); for (String word : testString.split(" ")) { deque.addFirst(word); } System.out.println(String.join(" ", deque)); } }
#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; int main() { vector<string>vec; string ss; while (cin >> ss) { vec.push_back(ss); } reverse(vec.begin(), vec.end()); vector<string>::iterator it; for (it = vec.begin(); it != vec.end(); it++) cout << *it << " "; cout << endl; return 0; }
自己测试的时候输入数据后按CTRL + Z退出循环就可以
#include <stdio.h> #include <string.h> int main( void ) { char str[1000]; gets( str ); char* sent_index = NULL; while( (sent_index = strrchr( str, ' ' )) != NULL ) { *sent_index = ' '; printf( "%s ", sent_index + 1 ); sent_index = NULL; } printf( "%s", str ); return 0; }
//两次翻转:先整体翻转,然后再每个单词翻转。 //时间O(n),空间O(1) #include<iostream> #include<algorithm> using namespace std; int main(){ string str; while(getline(cin,str)){ reverse(str.begin(), str.end()); str += ' '; //补上个' ' string::size_type s = 0, e = -1; string::iterator it = str.begin(); while((e = str.find(' ', e+1)) != string::npos){ reverse(it+s, it+e); s = e+1; } str.erase(str.end()-1); //删除补上的那个' ' cout<<str<<endl; } return 0; }
#include<iostream> #include<string> #include<vector> using namespace std; int main() { string str; while(getline(cin,str)) { vector<string>svec; string temp=str; int start=0,index=0; index=str.find(" ",start); while(index!=string::npos) { temp=str.substr(start,index-start); if(temp.size()>0) svec.push_back(temp); temp=""; start=index+1; index=str.find(" ",start); } temp=str.substr(start); if(temp.size()>0) svec.push_back(temp); for(int i=svec.size()-1;i>=1;--i) cout<<svec[i]<<" "; cout<<svec[0]<<endl; } return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String str = in.nextLine(); String[] sentences = str.split(" "); for (int i = sentences.length - 1; i >= 0; i--) { if (i==0) { System.out.print(sentences[i]); break; } System.out.print(sentences[i]+" "); } } } }
data=raw_input("").split() data.reverse() str="" for i in range(len(data)): if(i<len(data)-1): str=str+data[i]+" " else: str+=data[i] print str 下面是C++写的代码 #include<iostream> #include<string> #include<stack> using namespace std; int main() { string str; stack<string>ss; getline(cin, str); //cout << str << endl; int len = str.length(); string str1 = ""; for (int i = 0; i < len; i++) { if (str[i] == ' ') { if (i + 1 < len&&str[i + 1] != ' ') { ss.push(str1); str1 = ""; } } if (i == len - 1 && str1.length() != 0) { if (str[i] != ' ') { str1 += str[i]; } ss.push(str1); } else { if (str[i] != ' ') { str1 += str[i]; } } } while (!ss.empty()) { cout << ss.top(); ss.pop(); if (!ss.empty()) { cout << " "; } } //system("pause"); return 0; } 这段代码处理比较全,多个空格也没问题的,前面后面随意加空格也是没问题的哈
import java.util.*; public class Main { public static void main(String args[]){ Scanner cin=new Scanner(System.in); while(cin.hasNext()){ String s=cin.nextLine(); String[] array=s.split("[^a-zA-Z]"); List<String> list= Arrays.asList(array); Collections.reverse(list); for(String each:list.subList(0,list.size()-1)){ System.out.print(each+" "); } System.out.println(list.get(list.size()-1)); } } }
#include<stdio.h> #include<string.h> int main(void) { char a[1000]; int i; gets(a); for (i = strlen(a) - 1; i >= 0; i--) { if ((a[i]) == ' ') { printf("%s ", (a + i + 1)); a[i] = ' '; } else if (i == 0) printf("%s ", a); } return 0; }
#include <iostream> #include <string> #include <stack> using namespace std; int main() { string s; stack<string> sstack; while (cin >> s) { sstack.push(s); } while (sstack.size() != 1) { cout << sstack.top() << " "; sstack.pop(); } cout << sstack.top() << endl; return 0; }
#Python代码 print(' '.join((input().split())[::-1]))
print(‘ ‘.join(s[::-1]))
get_str = input() print(' '.join(get_str.split(' ')[::-1]))
lis=a.split()
for i in lis[::-1]:
print(i,end=’ ‘)
#include<iostream> #include<string> using namespace std; int main() { string s1; getline(cin,s1); s1=s1+' '; int end=s1.length()-1; for(int i=s1.length()-1;i>=0;i--) { if(s1[i]==' ') { for(int j=i+1;j<=end;j++) { cout<<s1[j]; } end=i; } } for(int j=0;j<=end;j++) { cout<<s1[j]; } }
以上就是关于问题将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I” 所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符
数据范围:输入的字符串长度满足
注意本题有多组输入的答案
欢迎关注区块链毕设网-
专业区块链毕业设计成品源码,定制。
区块链NFT链游项目方科学家脚本开发培训