#include<stdio.h> #include<string.h> void reverse(char* start, char* end) { char tmp; while (start < end) { tmp = *start; *start = *end; *end = tmp; start++; end--; } } int main() { char arr[100] = { 0 }; gets(arr); int len = strlen(arr); int i = 0; //先将字符串整体翻转 reverse(arr, arr + len - 1); char* a = arr; for (i = 0; i <=len; i++) { if (arr[i] == ' ' || arr[i] == '\0') { reverse(a, arr + i-1);//空格之前的再一次翻转,两次翻转正好 a = (arr + i + 1); } } printf("%s", arr); return 0; }
#include <stdio.h> #include <string.h> void ReverseString(char* left, char* right)//反转字符串 { while(left <= right) { char tmp = *left; *left = *right; *right = tmp; left++; right--; } } int main() { char str[100] = "0"; gets(str); int length = strlen(str); //将字符串整体翻转 ReverseString(str, str + length - 1); char* start = str; //将字符串中的每个单词翻转 while(*start) { char* end = start; while(*end != ' ' && *end)//寻找空格或'\0' { end++; } ReverseString(start, end - 1); if(*end == ' ') { start = end + 1; } else { start = end; } } printf("%s",str); return 0; }
int main() { char arr[100] = {0}; gets(arr); int len = strlen(arr); char* ptr = &arr[len-1]; while (ptr>arr) { if (*ptr==' ') { printf("%s", ptr+1); printf(" "); *ptr = '\0'; } ptr--; } printf("%s\n", ptr); return 0; }
//I like beijing. //整体翻转: //.gnijieb ekil I //单词翻转: //beijing. like I #include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string s; getline(cin, s); reverse(s.begin(), s.end());//整体翻转 auto first = s.begin(); while (first != s.end()) { auto last = first; while (last != s.end() && *last != ' ') { last++; } reverse(first, last);//单词翻转 if (last != s.end()) { first = last + 1; } else { first = last; } } cout << s <<endl; return 0; }
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string str; getline(cin,str); //进行整体的逆置 reverse(str.begin(), str.end()); auto start=str.begin(); while(start!=str.end()) { auto end=start; while(end != str.end() && *end != ' ') end++; //每个单词逆置 reverse(start,end); // start进行迭代 if(end!=str.end()) start=end+1; else start=str.end(); } cout << str; }
#include<stdio.h> #include<string.h> void reverse(char* l, char* r) { while (l < r) { char tmp = *l; *l = *r; *r = tmp; l++; r--; } } int main() { char arr[100] = { 0 }; gets(arr); int len = strlen(arr); char* left = arr; char* right = arr + len - 1; reverse(left,right); char* cur = arr; char* start = arr; while (*cur != '\0') { while ((*cur != ' ')&&(*cur!='\0')) { cur++; } reverse(start, cur - 1); start = cur + 1; cur++; } printf("%s", arr); return 0; }
public static void main(String[] args){ //每次读入一个字符串,以" "分隔, Scanner scanner=new Scanner(System.in); while(scanner.hasNext()){ String str= scanner.nextLine(); String[] arr=str.split(" "); //利用栈先进后出的特性 Stack<String> tmp=new Stack<>(); //将字符串数组加入到栈 for (int i = 0; i < arr.length; i++) { tmp.push(arr[i]); } while (!tmp.isEmpty()){ for (int i = 0 ; i < tmp.size(); i++) { //StringBuilder进行字符串拼接 StringBuilder stri=new StringBuilder(); stri.append(tmp.pop() + " "); System.out.print(stri); } } } }
#include<iostream> #include<string> using namespace std; int main() { string s; while(getline(cin,s)) { string str; int fast,slow; int sign=1; fast=slow=s.size()-1; while(fast!=0) { if(s[fast]==' ') { int tmp=fast; while(slow-tmp){ str+=s[tmp+1]; tmp++; } if(s[slow]!=' ') str+=s[fast]; slow=fast; } fast--; } while(slow-fast) { str+=s[fast]; fast++; } cout<<str<<endl; } return 0; }
//C语言答案 #include <stdio.h> #include <string.h> void sort(char *star,char *end) { while (star < end) { char p = *star; *star = *end; *end = p; star++; end--; } } int main() { char a[100]; char b[100]; int i; gets(a); int len = strlen(a); char *star = a; sort(a, a + len - 1); //俩次倒置即可 for (i = 0; i <= len; i++) { if (*(a + i) == ' '|| *(a + i)=='\0') { sort(star,a+i-1); star = (a + i+1); } } printf("%s", a); return 0; }
#include <stdio.h> #include <string.h> int main() { char s[300]={0}; fgets(s,300,stdin); unsigned int len=strlen(s); int flag=len-1;//记录第一个flag是换行 int i=0; int j=0; for(i=len-2;i>=0;i--)//从换行前一个字符开始找空格 { if(s[i-1]==' '||i==0)//i==0要特殊处理,因为s[-1]就越界了 { for(j=i;j<flag;j++) { printf("%c",s[j]); } flag=i-1;//记录空格位置 printf(" "); } } printf("\n"); return 0; }
import java.util.Scanner; public class Main { public static void reverse(char[] c,int i,int j) { while(i < j) { char tmp = c[i]; c[i] = c[j]; c[j] = tmp; i++; j--; } } public static void main(String[] args) { Scanner cin = new Scanner(System.in); String s = cin.nextLine(); s = s.trim(); char[] c = s.toCharArray(); reverse(c,0,c.length-1); int start = 0; for(int i = 0; i < c.length; i++) { if(c[i] == ' ') { reverse(c,start,i-1); start = i+1; } } reverse(c,start,c.length-1); System.out.println(new String(c)); } }
#include<stdio.h> #include<string.h> int main() { char arr[100]; int k = 0; int j = 0; gets(arr); int i = strlen(arr); int m = i - 1; for (k = i-1; k>=0; k--) { if (arr[k] == ' ') { for (j = k + 1; j <= m; j++) { printf("%c", arr[j]); if (j == (i - 1)) { printf(" "); } } m = k ; } if (k == 0) { for (j = 0; j < m; j++) { printf("%c", arr[j]); } } } return 0; }