首页 > 试题广场 >

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

[问答题]
给定字符串s, 要求把s中多于一个的连续空压缩成一个空格,并将连续的非空格字符串倒序打印出来,例如,给定"abc def efg",打印"cba fed gfe"
//使用栈来实现
#include <iostream>
#include <stack>
using namespace std;

int main()
{
stack<char> s;
int flag=0;
char ch=getchar();
while(1)
{
if(ch!='\n')
{
if(ch!=' '&&flag==0)
{
s.push(ch);
}
else
{
if(ch==' '&&flag==0)
{
while(s.size()!=0)
{
cout<<s.top();
s.pop();
}
cout<<" ";
flag=1;
}
else
{
if(ch!=' '&&flag==1)
{
flag=0;
s.push(ch);
}
}
}
ch=getchar();
}
else
{
while(s.size()!=0)
{
cout<<s.top();
s.pop();
}
cout<<endl;
break;
}
}

system("pause");
return 0;
}
发表于 2017-03-18 20:26:19 回复(0)
xxj头像 xxj
说明:两个循环,第一个循环去掉连续空格,第二个循环倒叙输出

void RPutString(char * src,int n)
{
    if(src==null&&n<2) return;

    char * first = src;
    char * second = src;
    for(int i=0;i<n;i++)
    {
        //second 先找到第一个非空格字符串
        if(*second != ' ')
            first++ = second;           
        else//*second==' '
            if(first != src && *(first-1) != ' ')
                first++ = ' ';
        ++second;
    }

    char * Rfirst = NULL;//用于指向新的末尾字符
    //设置结尾标记
    if(*first==' ')
    {
        *first='\0';
        Rfirst = first-1;
    }
    else
    {
        *(first+1)='\0';
        Rfirst = first;
    }

    //然后倒叙排列
    char temp;
    first = str;
    while(Rfirst > first)
    {
        temp = Rfirst;
        Rfirst = first;
        first = temp;
        ++first;
        --Rfirst;
    }
    
    //到此完成,最终字符串中头尾都非空格。
}
发表于 2014-11-27 10:01:41 回复(0)
#include <iostream>
#include <stack>
using namespace std;
void main()
{
    int i;
    int s;//字符串长度
    //int sign=0;
    char a[100];
    cin.get(a,100);
    s=strlen(a);
    stack <char>stk;
     for(i=0;i<s;i++)
     {
         if(a[i]!='  ')
         {
             stk.push(a[i]);
         }
         else
         {
             while(!stk.empty())
             {
                cout<<stk.top();
                stk.pop();
             }
             if(a[i]==a[i+1]&&a[i]=='  ')
  1. {}
  2.              else
  3.                 cout<<"  ";
  4.              
  5.          }    
  6.         
  7.         
  8.      }    
  9.      while(!stk.empty())
  10.     {
  11.         cout<<stk.top();
  12.         stk.pop();
  13.     }
  14.      cout<<endl;
  15. }

发表于 2019-03-24 19:51:16 回复(0)
import java.util.Scanner;
import java.util.Stack;
public class google1 {
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext())
        {
            String s=sc.nextLine();
            Stack<Character> stack=new Stack<Character>();
            String result="";
            
            //先处理头部和尾部
            int n=s.length();
            boolean last=false;
            if(s.charAt(0)==' ') result+=' ';
            if(n-1!=0 && s.charAt(n-1)==' ') last=true;
            s=s.trim();
            
            boolean flag=false;
            for(int i=0;i<s.length();i++)
            {
                if(s.charAt(i)!=' ')
                {
                    stack.push(s.charAt(i));
                    if(flag==true) flag=false;

                }else if(s.charAt(i)==' ' && flag==false)
                {
                    flag=true;
                    while(!stack.isEmpty())
                        result+=stack.pop();
                    result+=" ";
                }
                else
                    continue;
            }
            while(!stack.isEmpty())
                result+=stack.pop();
            
            if(last) result+=" "; 
            System.out.println(result);
        }
    }
}
发表于 2018-01-06 20:56:34 回复(0)
<pre class="prettyprint lang-cpp">string Zipper(string s){ string s1=""; string s2=""; int i=0,j=0,k=0,count=0; while(s[i]){ s1[i++]=s[j++]; if(s[j]==" ")j++; } for(i=0;i&lt;s1.length();i++){ if(s1[i]==' '){ for(k=count;k&lt;i;k++) s2[k]=s1[i-1-K]; s2[i]=" "; count=i+1; } } }</pre> <br />
发表于 2015-08-16 10:05:25 回复(0)
#include<iostream>
#include<cstring>
using namespace std;

int main()
{
    char a[100][100];
    int i = 0;
    while(cin >> a[i++]);
    for(int j = 0; j < i ; j ++)
    {
        for(int k = strlen(a[j]) - 1; k >= 0; k --)
        {
            cout << a[j][k];
        }
    cout << ' ';
    }
    return 0;
发表于 2015-07-31 16:33:00 回复(0)