首页 > 试题广场 >

给定字符串s, 要求把s中多于一个的连续空压缩成一个空格,并

[问答题]

给定字符串s, 要求把s中多于一个的连续空压缩成一个空格,并将连续的非空格字符串倒序打印出来,例如,给定"abc def efg",打印"cba fed gfe"

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{     string s;     getline(cin, s);     for(int i = 0, j , k; i < s.size(); ++i) {
        if(s[i] == ' ') {
            cout << ' ';
            for(j = i; j < s.size() && s[j] == ' '; ++j);
            i = j - 1;
        }
        else {
            string tmp;
            for(k = i; k < s.size() && s[k] != ' '; ++k) {
                tmp += s[k];
            }
            i = k - 1;
            reverse(tmp.begin(), tmp.end());
            cout << tmp;
            tmp.clear();
        }     }     return 0;
}

发表于 2018-02-19 22:24:19 回复(0)
package binarySearch;

public class SplitSpace {

    public static void main(String[] args) {
        String array = "abc def efg";
        String result = getResult(array);
        System.out.println("结果:"+result.toString());
    }
    
    private static String getResult(String array)
    {   
        String s ="";//初始化空字符串s
        String [] split = array.split(" ");//以空格分割字符串,一共三部分
        for(String str : split)//依次遍历分割后的部分
        {
            int strLen = str.length();//strLen:每部分的长度
            for(int i=1;i<=strLen;i++)
            {
                s+= str.charAt(strLen-i);//逆序查找每部分的元素,并加入字符串s
            }
            s += " ";    //每个部分逆序之后,加上一个空格    
        }
        return s;
    }
}

编辑于 2018-01-10 12:15:49 回复(0)
s = "abc def efg" def f(s):
    arr = s.split(' ')
    arr = filter(lambda x:len(x)>0,arr)
    arr = map(lambda x:x[::-1],arr)
    return ' '.join(arr)

发表于 2018-01-25 16:17:24 回复(0)
发表于 2021-08-27 20:43:53 回复(0)
# Python 就是简洁😂
s1 =s.split()
print("".join([t[::-1]+" " fort in s1]))
发表于 2019-05-28 00:37:31 回复(1)
void reverse(string &s){
    for(int i=s.size()-1;i>=0;--i){
         if(s[i]==' '){
             while(i>=0&&s[i--] == ' ');
              cout<<' ';
           }
          if(s[i] == ' ')
             break;  cout<<s[i];
    }
}
时间复杂度O(n),空间复杂度为O(1)
编辑于 2018-07-30 09:38:29 回复(1)
def f(s):
	a = s.strip().split()
	b = [x[::-1] for x in a]
	return ' '.join(b)

发表于 2018-04-10 23:50:02 回复(0)
s.split(' ').map{|e| e.reverse}.join(' ')

ruby

编辑于 2018-03-22 15:01:00 回复(0)
#include<stdio.h>
#include<string.h>
int main(){
    char ch[100];
    while(gets(ch)){
        if(strlen(ch)==0) break;
        for(int i=strlen(ch)-1;i>=0;i--){
            printf("%c",ch[i]);
        }
        printf("\n");
    
    }
    return 0;
}
发表于 2018-03-04 13:57:58 回复(0)
/* 给定字符串s, 要求把s中多于一个的连续空压缩成一个空格,并将连续的非空格字符串倒序打印出来,
 * 例如,给定"abc def efg",打印"cba fed gfe"
 */

#include <algorithm>
#include <iostream>
#include <string>

int main() {
  std::string s;
  std::getline(std::cin, s);
  auto NextSpace = std::find(s.begin(), s.end(), (const char)' ');
  auto NextNotSpace = std::find_if_not(s.begin(), s.end(), [](const char &sc) {
    return sc == ' ' ? true : false;
  });
  std::string Output;
  if (NextSpace == s.begin()) {
    Output.push_back(' ');
  }
  while (NextSpace != s.end() && NextNotSpace != s.end()) {
    NextSpace = std::find(NextNotSpace, s.end(), (const char)' ');
    for (auto i = NextSpace - 1; i >= NextNotSpace; --i) {
      Output.push_back(*i);
    }
    NextNotSpace = std::find_if_not(NextSpace, s.end(), [](const char &sc) {
      return sc == ' ' ? true : false;
    });
    Output.push_back(' ');
  }
  std::cout << Output << std::endl;
  return 0;
}
VS 2017 会报异常但是 g++ 不会,很神奇
编辑于 2018-02-28 20:29:47 回复(0)
人生苦短,我用python
def strtest(s):
    a = s.split()  #消去空格
    b=""
    for i in a:
        b+=i[::-1]  #每一段连续的字符倒序
        b+=" "  #分割的地方加一个空格
    return b
发表于 2018-01-30 20:26:53 回复(0)
void solution(string s) {
 string tmp("");
 stack<char> sta;
 bool change = false;
 int len = (int)s.length();
 for (int i = 0; i < len; i++) {
 while (s[i] == ' ' && i < len) {
 change = true;
 i++;
 }
 if (change) {
 tmp.push_back(' ');
 change = false;
 while (!sta.empty()) {
 cout << sta.top();
 sta.pop();
 }
 cout << " ";
 }
 if (i < len) {
 tmp.push_back(s[i]);
 sta.push(s[i]);
 }
 }
 cout << endl;
}
发表于 2018-01-17 20:39:47 回复(0)
    public String strReversal(String s){
        s =s.replaceAll(" +", " ");
        String[] split = s.split(" ");
        s = "";
        for(String str:split){
            int strLen = str.length();
            for(int i=1;i<=strLen;i++){
                s+=str.charAt(strLen-i);
            }
            s+=" ";
        }
        return s;
    }
发表于 2018-01-05 22:40:33 回复(0)