a********r 发帖数: 218 | 1 Question:
reverse the string based on the word (or you can understand as reversing
words), for example,
Input string: I Am Engineer
Output string shall be: Engineer Am I
我是菜鸟, 那位达人能用C/C++编码一下,跪谢! |
B*****g 发帖数: 34098 | 2 自己搜搜吧,这个是老题了。
【在 a********r 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Question: : reverse the string based on the word (or you can understand as reversing : words), for example, : Input string: I Am Engineer : Output string shall be: Engineer Am I : 我是菜鸟, 那位达人能用C/C++编码一下,跪谢!
|
l*****a 发帖数: 559 | 3 no offence.
if you cannot find a solution to this question, you shall not apply a sde in
MS. |
a********r 发帖数: 218 | 4 你能不能帮忙给个答案呢
【在 B*****g 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 自己搜搜吧,这个是老题了。
|
a********r 发帖数: 218 | 5 I agree, could you please code it if it does not bother you too much?
in
【在 l*****a 的大作中提到】![](/moin_static193/solenoid/img/up.png) : no offence. : if you cannot find a solution to this question, you shall not apply a sde in : MS.
|
s********x 发帖数: 914 | 6 // input I AM Engineer
// output Engineer AM I
public static char[] reverseWordinString(char[] string){
char[] result = new char[string.length];
int k = 0;
for (int i = string.length-1; i >=0 ; ){
if (!isWord(string[i])) {
int j = i;
while (j>=0&&!isWord(string[j])) j-- ;
if (j==0 && !isWord(string[0]))
result[k++] = string[0];
for (int t = j + 1 ; t <= i ; t ++)
result[k++] = string[t];
i = j;
} else{
int j = i;
while (j>=0&&isWord(string[j])) j-- ;
if (j==0 && isWord(string[0]))
result[k++] = string[0];
for (int t = j + 1 ; t <= i ; t ++)
result[k++] = string[t];
i = j;
}
}
return result;
}
private static boolean isWord(char c) {
if ( (int) c >= (int) 'a' && (int) c <= (int) 'z')
return true;
if ( (int) c >= (int) 'A' && (int) c <= (int) 'Z')
return true;
return false;
}
【在 a********r 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I agree, could you please code it if it does not bother you too much? : : in
|
s********x 发帖数: 914 | 7 String s = "Hello, my name is John";
char[] r = reverseWordinString(s.toCharArray());
for (int i = 0 ; i< r.length; i++){
System.out.print(r[i]);
}
John is name my, Hello
【在 s********x 的大作中提到】![](/moin_static193/solenoid/img/up.png) : // input I AM Engineer : // output Engineer AM I : public static char[] reverseWordinString(char[] string){ : char[] result = new char[string.length]; : int k = 0; : for (int i = string.length-1; i >=0 ; ){ : if (!isWord(string[i])) { : int j = i; : while (j>=0&&!isWord(string[j])) j-- ; : if (j==0 && !isWord(string[0]))
|
s********x 发帖数: 914 | 8 有包子没?
【在 s********x 的大作中提到】![](/moin_static193/solenoid/img/up.png) : String s = "Hello, my name is John"; : char[] r = reverseWordinString(s.toCharArray()); : for (int i = 0 ; i< r.length; i++){ : System.out.print(r[i]); : } : John is name my, Hello
|
S**I 发帖数: 15689 | 9 LZ ask for C/C++, not C#.
【在 s********x 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 有包子没?
|
j**l 发帖数: 2911 | 10 Programming Interview Exposed |
|
|
a********r 发帖数: 218 | 11 Thanks a lot!
But I need an implementation using C/C++, you cannot even use STL. Also, it
is better to use in place replacement.
【在 s********x 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 有包子没?
|
S**I 发帖数: 15689 | 12 #include
#include
void reverseStr(char str[], int begin, int end){
char temp;
while(end > begin){
temp = str[begin];
str[begin++] = str[end];
str[end--] = temp;
}
}
void reverseWord(char str[]){
int begin = 0, end = 0, length = (int) strlen(str) - 1;
reverseStr(str, begin, length);
while(end < length){
if(str[end] != ' '){
begin = end;
while(end < length && str[end] != ' ')
end++;
reverseStr(str, begin, end - 1);
}
end++;
}
}
int main(int argc, const char* argv[])
{
char str[100];
strcpy(str, argv[1]);
reverseWord(str);
printf("%s", str);
}
【在 a********r 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Question: : reverse the string based on the word (or you can understand as reversing : words), for example, : Input string: I Am Engineer : Output string shall be: Engineer Am I : 我是菜鸟, 那位达人能用C/C++编码一下,跪谢!
|
h*********n 发帖数: 11319 | 13 i believe the output should depend on how the interviewer would like to trea
t ' ' (or other delimiters)
string reverseByWord(string in){
string out;
string::iterator s = in.end();
string::iterator e = in.end();
while(s!=in.begin()){
e = s;
s--;
while((*s != ' ')&&(s != in.begin())){
s--;//strops at space
}
string::iterator cur = s;
if(s!=in.begin())
cur++;
for(;cur!=e; cur++)
out.push_back(*cur);
if(s!=in.begin())
out.push_back(' ');
}
return out;
}
【在 s********x 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 有包子没?
|
W****X 发帖数: 84 | |
a********r 发帖数: 218 | 15 Thanks SETI!
【在 S**I 的大作中提到】![](/moin_static193/solenoid/img/up.png) : #include : #include : void reverseStr(char str[], int begin, int end){ : : char temp; : : while(end > begin){ : temp = str[begin]; : str[begin++] = str[end]; : str[end--] = temp;
|
l*****a 发帖数: 559 | 16 There need to have a minor change.
From
while(end < length && (str[end] != ' '))
end++;
To
while(end <= length && (str[end] != ' '))
end++;
【在 S**I 的大作中提到】![](/moin_static193/solenoid/img/up.png) : #include : #include : void reverseStr(char str[], int begin, int end){ : : char temp; : : while(end > begin){ : temp = str[begin]; : str[begin++] = str[end]; : str[end--] = temp;
|
a********r 发帖数: 218 | 17 我是菜鸟,Lanmama 达人:
Can you implement it with in-place word-by-word swapping? for example,
considering using two pointers, one move from left to right, another move
from right to left.
【在 l*****a 的大作中提到】![](/moin_static193/solenoid/img/up.png) : There need to have a minor change. : From : while(end < length && (str[end] != ' ')) : end++; : To : while(end <= length && (str[end] != ' ')) : end++;
|
d**e 发帖数: 6098 | 18 前面SETI的不是in-place?
【在 a********r 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 我是菜鸟,Lanmama 达人: : Can you implement it with in-place word-by-word swapping? for example, : considering using two pointers, one move from left to right, another move : from right to left.
|
S**I 发帖数: 15689 | 19 you're right, just changed.
【在 l*****a 的大作中提到】![](/moin_static193/solenoid/img/up.png) : There need to have a minor change. : From : while(end < length && (str[end] != ' ')) : end++; : To : while(end <= length && (str[end] != ' ')) : end++;
|
a********r 发帖数: 218 | 20 Yes, SETI's is in-place and char-by-char swapping. possible to word-by-word
swapping?
【在 d**e 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 前面SETI的不是in-place?
|
d**e 发帖数: 6098 | 21 做法都这样的,直接换word不行
word
【在 a********r 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Yes, SETI's is in-place and char-by-char swapping. possible to word-by-word : swapping?
|
f********3 发帖数: 210 | 22 我也是新人,呵呵。不过刚好做过这道题。
有包子没?=D
#include
using namespace std;
using std::string;
char *reverseEachword(char *src, int len)
{
char *p = src, *q = src + len - 1;
char tmp;
while(p < q)
{
tmp = *p;
//src[0] = 'z';
*p = *q; // why this step is wrong?
*q = tmp;
p++;
q--;
}
return src;
}
char *reverseWholeStr(char *srcstr, int len)
{
char *p = srcstr, *q;
int count;
while(*p != '\0')
{
count = 0;
q = p;
while(*q != ' ' && *q != '\0')
{
q++;
count++;
}
reverseEachword(p, count);
if(*(p+count) != '\0')
p += count+1;
else
p += count;
}
return srcstr;
}
int main()
{
char ss[] = "Hello this is Bob";
char *t = reverseEachword(ss, 17);
char *dst = reverseWholeStr(t, 17);
for(char *p = dst; *p != '\0'; p++)
cout<<*p;
cout<
}
as reversing
【在 a********r 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Question: : reverse the string based on the word (or you can understand as reversing : words), for example, : Input string: I Am Engineer : Output string shall be: Engineer Am I : 我是菜鸟, 那位达人能用C/C++编码一下,跪谢!
|