Need a program to reverse the words in a string

时间秒杀一切 提交于 2019-12-06 13:01:01

问题


I asked this question in a few interviews. I want to know from the Stackoverflow readers as to what should be the answer to this question.

Such a seemingly simple question, but has been interpreted quite a few different ways.


回答1:


if your definition of a "word" is a series of non-whitespace characters surrounded by a whitespace character, then in 5 second pseudocode you do:

var words = split(inputString, " ")
var reverse = new array
var count = words.count -1
var i = 0
while count != 0
   reverse[i] = words[count]
   count--
   i++
return reverse



回答2:


If you want to take into consideration also spaces, you can do it like that:

   string word = "hello    my name   is";
   string result="";
   int k=word.size();
   for (int j=word.size()-1; j>=0; j--)
   {       
       while(word[j]!= ' ' && j>=0)
           j--;

       int end=k;
       k=j+1;
       int count=0;
       if (j>=0)  
       {        
           int temp=j;
           while (word[temp]==' '){
               count++;
               temp--;           
           }           
           j-=count;
       }        
       else j=j+1;       

       result+=word.substr(k,end-k);
       k-=count;
       while(count!=0)
       {
           result+=' ';
           count--;
       }
   }  

It will print out for you "is name my hello"




回答3:


Taken from something called "Hacking a Google Interview" that was somewhere on my computer ... don't know from where I got it but I remember I saw this exact question inside ... here is the answer:

Reverse the string by swapping the first character with the last character, the second with the second-to-last character, and so on. Then, go through the string looking for spaces, so that you find where each of the words is. Reverse each of the words you encounter by again swapping the first character with the last character, the second character with the second-to-last character, and so on.




回答4:


This came up in LessThanDot Programmer Puzzles




回答5:


#include<stdio.h>
void reverse_word(char *,int,int);
int main()
{
char s[80],temp;
int l,i,k;
int lower,upper;
printf("Enter the ssentence\n");
gets(s);
l=strlen(s);
printf("%d\n",l);
k=l;
for(i=0;i<l;i++)
{
if(k<=i)
{temp=s[i];
s[i]=s[l-1-i];
s[l-1-i]=temp;}
k--;
}
printf("%s\n",s);
lower=0;
upper=0;
for(i=0;;i++)
{
if(s[i]==' '||s[i]=='\0')
{upper=i-1;
reverse_word(s,lower,upper);
lower=i+1;
}
if(s[i]=='\0')
break;
}
printf("%s",s);
return 0;
}

void reverse_word(char *s,int lower,int upper)
{
char temp;
//int i;
while(upper>lower)
{
temp=s[lower];
s[lower]=s[upper];
s[upper]=temp;
upper=upper-1;
lower=lower+1;
}
}



回答6:


The following code (C++) will convert a string this is a test to test a is this:

string reverseWords(string str)
{
    string result = "";

    vector<string> strs;
    stringstream S(str);
    string s;
    while (S>>s)
        strs.push_back(s);

    reverse(strs.begin(), strs.end());

    if (strs.size() > 0)
        result = strs[0];
    for(int i=1; i<strs.size(); i++)
        result += " " + strs[i];

    return result;
}

PS: it's actually a google code jam question, more info can be found here.



来源:https://stackoverflow.com/questions/1647247/need-a-program-to-reverse-the-words-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!