使用c语言中的一行代码进行字符串反转

时间:2022-09-30 20:50:56

During a recent interview one of my friends was asked a question to reverse a string using only a single line of code. The string is already stored in a char* variable.

在最近的一次采访中,我的一个朋友被问到一个问题,用一行代码就可以反转一个字符串。字符串已经存储在char*变量中。

I tried using recursion, but it never reduced to a single line of code.

我尝试过使用递归,但是它从来没有简化成一行代码。

Could anyone help me with this?

有人能帮我吗?

5 个解决方案

#1


7  

  1. Write a program which works.
  2. 写一个可以工作的程序。
  3. Condense it all onto a single line. You don't need to have linefeeds between statements!
  4. 把它们压缩成一行。您不需要在语句之间使用换行符!

#2


5  

If you really want it in C, and strlen is allowed, this will work:

如果你真的想要它在C中,并且strlen是被允许的,这将会起作用:

for( char *p = str, *q = str + strlen(str) - 1; p < q; *p ^= *q, *q ^= *p, *p++ ^= *q-- );

I do not recommend you to use this in any actual program... Anyone got an idea how to get rid of the strlen?

我不建议你在任何实际的程序中使用这个。有人知道怎么去掉strlen吗?

Edit: even more beautiful:

编辑:更漂亮:

for( char *p = str, *q = strchr(str, 0) - 1, t; p < q; t = *p, *p++ = *q, *q++ = t );

#3


3  

char str[] = "test";

std::reverse(str, str + std::strlen(str));

#4


1  

This seems to work fine

这似乎没问题。

void reverse(char *s, int len) { \
  if(len <= 1) \
    return; \
  char t; \
  char *e = s + len - 1; \
  while(s < e) { \
    t = *s; \
    *s = *e; \
    *e = t; \ 
    e--; \
    s++; \
  } \
}

Note that his code is all in one line. Prefer code clarity. If you can put a function's code into one line using language features, then use those features.

注意,他的代码都在一行中。喜欢清晰的代码。如果可以使用语言特性将函数的代码放在一行中,那么就使用这些特性。

#5


0  

It requires an ugly construct involving a while loop with character pointer arithmetic and both post-increment and post-decrement operators. Never, ever use such hideous code except in obfuscation contests. (No, not even in interviews. If they ask you such useless trivia, chances are they are equally clueless about other things.)

它需要一个丑陋的结构,包含一个带有字符指针算法的while循环和后递增和后递减操作符。永远不要使用这种丑陋的代码,除非在混淆比赛中。(不,即使是在面试中。如果他们问你这些无用的琐事,很可能他们对其他事情同样一无所知。

#1


7  

  1. Write a program which works.
  2. 写一个可以工作的程序。
  3. Condense it all onto a single line. You don't need to have linefeeds between statements!
  4. 把它们压缩成一行。您不需要在语句之间使用换行符!

#2


5  

If you really want it in C, and strlen is allowed, this will work:

如果你真的想要它在C中,并且strlen是被允许的,这将会起作用:

for( char *p = str, *q = str + strlen(str) - 1; p < q; *p ^= *q, *q ^= *p, *p++ ^= *q-- );

I do not recommend you to use this in any actual program... Anyone got an idea how to get rid of the strlen?

我不建议你在任何实际的程序中使用这个。有人知道怎么去掉strlen吗?

Edit: even more beautiful:

编辑:更漂亮:

for( char *p = str, *q = strchr(str, 0) - 1, t; p < q; t = *p, *p++ = *q, *q++ = t );

#3


3  

char str[] = "test";

std::reverse(str, str + std::strlen(str));

#4


1  

This seems to work fine

这似乎没问题。

void reverse(char *s, int len) { \
  if(len <= 1) \
    return; \
  char t; \
  char *e = s + len - 1; \
  while(s < e) { \
    t = *s; \
    *s = *e; \
    *e = t; \ 
    e--; \
    s++; \
  } \
}

Note that his code is all in one line. Prefer code clarity. If you can put a function's code into one line using language features, then use those features.

注意,他的代码都在一行中。喜欢清晰的代码。如果可以使用语言特性将函数的代码放在一行中,那么就使用这些特性。

#5


0  

It requires an ugly construct involving a while loop with character pointer arithmetic and both post-increment and post-decrement operators. Never, ever use such hideous code except in obfuscation contests. (No, not even in interviews. If they ask you such useless trivia, chances are they are equally clueless about other things.)

它需要一个丑陋的结构,包含一个带有字符指针算法的while循环和后递增和后递减操作符。永远不要使用这种丑陋的代码,除非在混淆比赛中。(不,即使是在面试中。如果他们问你这些无用的琐事,很可能他们对其他事情同样一无所知。