反转一个字符串 [英] Reverse a string

查看:80
本文介绍了反转一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数微软访谈中,这个问题都是模态提问的。我开始考虑各种各样的实现。
开始考虑它的各种实现。这就是我得到的



#include< stdio.h>

#include< stdlib.h>

#include< string.h>

char * StrReverse(char *);

char * StrReverse1(char *);

char * StrReverse2(char *);

void StrReverse3(char *);

void StrReverse4(char *);


int main(无效)

{


char str [50];

int temp = 0 ;


printf("输入字符串:");

scanf("%s",str);

printf("字符串的反向是:%s \ n",StrReverse(str));

printf("字符串的反向是:%s \ n" ;,StrReverse1(str));

printf("字符串的反向是:%s \ n",StrReverse2(str));


StrReverse3(str);

printf("字符串的反向是:%s \ n",str);


//取回原来的字符串

StrReverse3(str);


//再次反转

printf("字符串的反面是:");

StrReverse4(str);

printf(" \ n");


scanf("%d",& temp);


}

char * StrReverse(char * str)

{

char * temp,* ptr;

int len,i;


temp = str;

for(len = 0; * temp!=''\'''; temp ++,len ++);


ptr = malloc(sizeof(char)*(len + 1));
< ($ = len-1; i> = 0; i--)

ptr [len-i-1] = str [i];


ptr [len] =''\ 0'';

返回ptr;

}


char * StrReverse1(char * str)

{

char * temp,* ptr;

int len,i;


temp = str;

for(len = 0; * temp!=''\'''; temp ++,len ++);


ptr = malloc(sizeof(char)*(len + 1));


for(i = len-1; i> = 0; i- - )

*(ptr + len-i-1)= *(str + i);


*(ptr + len)=''\\ \\ 0'';

返回ptr;

}


char * StrReverse2(char * str)

{

int i,j,len;

char temp;

char * ptr = NULL;

i = j = len = temp = 0;


len = strlen(str);

ptr = malloc(sizeof(char)*(len + 1));

ptr = strcpy(ptr,str);

for(i = 0,j = len-1; i< = j; i ++, j--)

{

temp = ptr [i];

ptr [i] = ptr [j];

ptr [j] = temp;

}

返回ptr;

}


void StrReverse3(char * str)

{

int i,j,len;

char temp;

i = j = len = temp = 0;


len = strlen(str);

for(i = 0,j = len-1; I< = j的; i ++,j--)

{

temp = str [i];

str [i] = str [j];

str [j] = temp;

}

}


/ *一种coooooooooool的逆转方式递归的字符串。我在这个网址找到了这个


http://www.geocities.com/cyberkabila...ersestring.htm

* /


void StrReverse4(char * str)

{

if(* str)

{

StrReverse4(str + 1); < br $> b $ b putchar(* str);

}

}


然后,我读了一个人说一个字符串可以用一个OR运算符在单个

扫描中反转。从那以后我一直渴望

知道如何。如果有人可以请与我分享,使用XOR操作符反转

字符串的代码,我将不胜感激。


问候,

Sathyaish

This one question is asked modally in most Microsoft interviews. I
started to contemplate various implementations for it. This was what I
got.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* StrReverse(char*);
char* StrReverse1(char*);
char* StrReverse2(char*);
void StrReverse3(char*);
void StrReverse4(char*);

int main(void)
{

char str[50];
int temp=0;

printf("Enter a string: ");
scanf("%s", str);
printf("The reverse of the string is: %s\n", StrReverse(str));
printf("The reverse of the string is: %s\n", StrReverse1(str));
printf("The reverse of the string is: %s\n", StrReverse2(str));

StrReverse3(str);
printf("The reverse of the string is: %s\n", str);

//Get back the original string
StrReverse3(str);

//Reverse it again
printf("The reverse of the string is: ");
StrReverse4(str);
printf("\n");

scanf("%d", &temp);

}
char* StrReverse(char* str)
{
char *temp, *ptr;
int len, i;

temp=str;
for(len=0; *temp !=''\0'';temp++, len++);

ptr=malloc(sizeof(char)*(len+1));

for(i=len-1; i>=0; i--)
ptr[len-i-1]=str[i];

ptr[len]=''\0'';
return ptr;
}

char* StrReverse1(char* str)
{
char *temp, *ptr;
int len, i;

temp=str;
for(len=0; *temp !=''\0'';temp++, len++);

ptr=malloc(sizeof(char)*(len+1));

for(i=len-1; i>=0; i--)
*(ptr+len-i-1)=*(str+i);

*(ptr+len)=''\0'';
return ptr;
}

char* StrReverse2(char* str)
{
int i, j, len;
char temp;
char *ptr=NULL;
i=j=len=temp=0;

len=strlen(str);
ptr=malloc(sizeof(char)*(len+1));
ptr=strcpy(ptr,str);
for (i=0, j=len-1; i<=j; i++, j--)
{
temp=ptr[i];
ptr[i]=ptr[j];
ptr[j]=temp;
}
return ptr;
}

void StrReverse3(char* str)
{
int i, j, len;
char temp;
i=j=len=temp=0;

len=strlen(str);
for (i=0, j=len-1; i<=j; i++, j--)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}

/*A coooooooooool way of reversing a string by recursion. I found it
at this web address
http://www.geocities.com/cyberkabila...ersestring.htm
*/

void StrReverse4(char *str)
{
if(*str)
{
StrReverse4(str+1);
putchar(*str);
}
}

Then, I read one guy saying a string could be reversed in one single
sweep with the exclusive OR operator. Since then I''ve been itching to
know how. If someone can please share with me, the code to reverse a
string with the XOR operator, I''ll be grateful.

Regards,
Sathyaish

推荐答案

Sathyaish写道:


[多次狙击]
Sathyaish wrote:

[much snippage]

然后,我读了一个人说一个字符串可以用一个OR运算符在一次扫描中被反转。从那时起,我一直渴望知道如何。如果有人可以请与我分享,使用XOR操作符反转
字符串的代码,我将不胜感激。

Then, I read one guy saying a string could be reversed in one single
sweep with the exclusive OR operator. Since then I''ve been itching to
know how. If someone can please share with me, the code to reverse a
string with the XOR operator, I''ll be grateful.




你可以交换到chars,a和b,用:


* a ^ = * b;

* b ^ = * a;

* a ^ = * b;


担心字符串中间会发生什么,当a和b有相同的地址

时。


-

rh



You can swap to chars, a and b, with:

*a ^= *b;
*b ^= *a;
*a ^= *b;

Worry about what happens in the middle of the string, when a and b have
the same address.

--
rh


Sathyaish写道:
Sathyaish wrote:

在大多数微软访谈中,这个问题是模态提问的。我开始考虑各种各样的实现。这就是我得到的。

/ *一种通过递归来反转字符串的coooooooooool方法。我在这个网址找到了它
http ://www.geocities.com/cyberkabila...ersestring.htm
* /

void StrReverse4(char * str)
{
if(* str)
{
StrReverse4(str + 1);
putchar(* str);
}
}

然后,我读了一个人说一个字符串可以用一个OR运算符在一次扫描中反转。从那时起,我一直渴望知道如何。如果有人可以请与我分享,使用XOR操作符反转
字符串的代码,我将不胜感激。

问候,
Sathyaish

This one question is asked modally in most Microsoft interviews. I
started to contemplate various implementations for it. This was what I
got.

/*A coooooooooool way of reversing a string by recursion. I found it
at this web address
http://www.geocities.com/cyberkabila...ersestring.htm
*/

void StrReverse4(char *str)
{
if(*str)
{
StrReverse4(str+1);
putchar(*str);
}
}

Then, I read one guy saying a string could be reversed in one single
sweep with the exclusive OR operator. Since then I''ve been itching to
know how. If someone can please share with me, the code to reverse a
string with the XOR operator, I''ll be grateful.

Regards,
Sathyaish




递归的字符串反转使用O(n ^ 2)时间。您可以通过将指针L和U保持在末尾来在O(n)

时间内完成。在伪代码中,


而L< U

swap(s [L],s [u]);

L = L + 1;

U = U-1;

重复

-

Julian V. Noble < br $>
物理荣誉教授
jv*@lessspamformother.virginia.edu

^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/


因为从来没有哲学家可以忍受牙痛

耐心等待。 - 嗯。莎士比亚,很多阿多无所事事。法案诉Sc。 1.



String reversal by recursion uses O(n^2) time. You can do it in O(n)
time by keeping pointers L and U to the ends. In pseudocode,

while L<U
swap( s[L], s[u] );
L = L+1;
U = U-1;
repeat
--
Julian V. Noble
Professor Emeritus of Physics
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"For there was never yet philosopher that could endure the toothache
patiently." -- Wm. Shakespeare, Much Ado about Nothing. Act v. Sc. 1.




2004年7月21日星期三,Julian V. Noble写道:

On Wed, 21 Jul 2004, Julian V. Noble wrote:

Sathyaish写道:

Sathyaish wrote:
/ *通过递归来反转字符串的coooooooooool方法。我在这个网址找到了它
http ://www.geocities.com/cyberkabila...ersestring.htm
* /

void StrReverse4(char * str)
{
if(* str)
{
StrReverse4(str + 1);
putchar(* str);
}
}
通过递归使用的字符串反转O(n ^ 2)时间。
/*A coooooooooool way of reversing a string by recursion. I found it
at this web address
http://www.geocities.com/cyberkabila...ersestring.htm
*/

void StrReverse4(char *str)
{
if(*str)
{
StrReverse4(str+1);
putchar(*str);
}
}
String reversal by recursion uses O(n^2) time.




不是Sathyaish这样做的方式;那是'O(n)就在那里(即使它打印出来的东西,而不是反转字符串)。

你可以在O(n)中做到这一点
时间将指针L和U保持在最后。在伪代码中,

而L< U
交换(s [L],s [u]);
L = L + 1;
U = U- 1;
重复



Not the way Sathyaish is doing it; that''s O(n) right there (even
though it prints out something, rather than reversing the string).
You can do it in O(n)
time by keeping pointers L and U to the ends. In pseudocode,

while L<U
swap( s[L], s[u] );
L = L+1;
U = U-1;
repeat




或者,将循环转换为尾递归,


void revmem(char * s,int L,int U)

{

if(L< U){

swap(s [L],s [ U-1]);

revmem(s,L + 1,U-1);

}

}


(注意U的含义的变化。)



Or, transforming the loop into tail-recursion,

void revmem(char *s, int L, int U)
{
if (L < U) {
swap(s[L], s[U-1]);
revmem(s, L+1, U-1);
}
}

(Note the change in meaning of U. :)

然后,我读了一个人说使用异或运算符可以在单个扫描中反转字符串。从那时起,我一直渴望知道如何。如果有人可以请与我分享,使用XOR操作符反转
字符串的代码,我将不胜感激。
Then, I read one guy saying a string could be reversed in one single
sweep with the exclusive OR operator. Since then I''ve been itching to
know how. If someone can please share with me, the code to reverse a
string with the XOR operator, I''ll be grateful.




这家伙可能正在考虑老栗子


x ^ = y ^ = x ^ = y;


这不仅是无效的C代码,但是如果& x ==& y就不起作用。

并且它只是交换了两个值(假设它确实显示了那个人显然是b $ b b预期它做);虽然交换是字符串反转的重要组成部分,但是它不是完全一样的。它与

" sweeps无关。


-Arthur



The guy was probably thinking of the old chestnut

x ^= y ^= x ^= y;

which is not only invalid C code, but doesn''t work if &x == &y.
And that just swaps two values (assuming it does what the guy obviously
expected it to do); while swapping is a big part of string reversal,
it''s not exactly the same thing. And it has nothing to do with
"sweeps."

-Arthur


这篇关于反转一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆