Is this function the fastest, most optimized way of reversing a string in C? This runs in O(n/2) time. The optimization is that it only iterates through half of the string.
这个函数是C中反转字符串的最快,最优化的方法吗?这在O(n / 2)时间内运行。优化是它只迭代字符串的一半。
char* str_reverse(char *str, int len)
{
char word[len];
int i;
for (i = 0; i <= len / 2; ++i) {
word[i] = str[len - i - 1];
word[len - i - 1] = str[i];
}
word[len] = '\0';
return word;
}
6 个解决方案
#1
11
Maybe something like this?
也许是这样的?
char *str_reverse_in_place(char *str, int len)
{
char *p1 = str;
char *p2 = str + len - 1;
while (p1 < p2) {
char tmp = *p1;
*p1++ = *p2;
*p2-- = tmp;
}
return str;
}
#2
3
You'll find algorithms taking less instructions, like this in place reverse
您会发现算法采用较少的指令,例如反向操作
char* str_reverse_in_place(char *str, int len)
{
int i;
for (i = len/2-1 ; i >= 0 ; --i) {
char c = str[i];
str[i] = str[len-i-1];
str[len-i-1] = c;
}
return str;
}
Optimizing for speed at that level, look at the inline keyword, also compile with (for gcc) with -O3
(does usually a better job that adding register ... by yourself).
优化该级别的速度,查看内联关键字,也可以使用-O3编译(对于gcc)(通常自己添加寄存器更好)。
If you need to have the reversed string elsewhere, either provide it in the function (being allocated for strlen(str)+1
- actually len+1
here - characters)
如果你需要在其他地方使用反向字符串,或者在函数中提供它(为strlen(str)+1分配 - 实际上是len + 1这里 - 字符)
char* str_reverse(char *str, char *reverse, int len)
{
int i;
for (i = len-1 ; i >= 0 ; --i) {
reverse[i] = str[len-i-1];
}
reverse[len] = 0;
return reverse;
}
or malloc it (it will have to be freed by the caller).
或malloc它(它必须由调用者释放)。
char* str_reverse_malloc(char *str, int len)
{
char *reverse = malloc(len+1);
if ( ! reverse) return NULL;
int i;
for (i = len-1 ; i >= 0 ; --i) {
reverse[i] = str[len-i-1];
}
reverse[len] = 0;
return reverse;
}
#3
3
The "most" optimized way must address the question of CPU and memory architecture as well as what is being reversed (long strings or short strings and what is the distribution).
“最”优化的方法必须解决CPU和内存架构以及正在逆转的问题(长字符串或短字符串以及分布是什么)。
There's no way to get relax the O(N) requirement, but one can use techniques such as loop unrolling, loop blocking and parallelism to optimize cache misses for very large strings. Also one can increase the word size and swap words, dwords or larger entities in-place (while dealing with the probable alignment issue).
没有办法放松O(N)要求,但可以使用诸如循环展开,循环阻塞和并行性等技术来优化非常大的字符串的缓存未命中。还可以增加字大小并就地交换字,双字或更大的实体(同时处理可能的对齐问题)。
// This will most likely be faster than byte-wise copying, but it's not O(N/8)...
//这很可能比逐字节复制更快,但它不是O(N / 8)......
if (len & 7 == 0)
{
uint32_t *dst = src+len-4;
uint32_t *src = (uint32_t *)ptr;
while (src<dst)
{
a = *src; b = *dst;
*src++ = byte_swap(b);
*dst-- = byte_swap(a);
}
}
#4
2
int main() {
char str[100], temp;
int i, j = 0;
printf("\nEnter the string :");
gets(str);
i = 0;
j = strlen(str) - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("\nReverse string is :%s", str);
return (0);
}
#5
0
Here is a variation that does not require the length to be passed and will swap both the beginning and ending characters at a given offset within the string each pass through the loop:
这是一个不需要传递长度的变体,它将在每个通过循环的字符串中的给定偏移处交换开始和结束字符:
/** strrevstr - reverse string, swaps src & dest each iteration.
* Takes valid string and reverses, original is not preserved.
* If str is valid, returns pointer to str, NULL otherwise.
*/
char *strrevstr (char *str)
{
if (!str) {
printf ("strrevstr() error: invalid string\n");
return NULL;
}
char *begin = str;
char *end = str + strlen (str) - 1;
char tmp;
while (end > begin)
{
tmp = *end;
*end-- = *begin;
*begin++ = tmp;
}
return str;
}
#6
-1
This is the fastest way to print a reverse string in c
这是在c中打印反向字符串的最快方法
#include <stdio.h>
#include <string.h>
int main()
{
char str[30],str1[30];
printf("Enter string :");
scanf("%s",str);
int i = strlen(str);
for (int j = 0; j <=5;j++)
{
str1[j]=str[i-j];
printf("%c",str1[j]);
}
printf("\n");
}
#1
11
Maybe something like this?
也许是这样的?
char *str_reverse_in_place(char *str, int len)
{
char *p1 = str;
char *p2 = str + len - 1;
while (p1 < p2) {
char tmp = *p1;
*p1++ = *p2;
*p2-- = tmp;
}
return str;
}
#2
3
You'll find algorithms taking less instructions, like this in place reverse
您会发现算法采用较少的指令,例如反向操作
char* str_reverse_in_place(char *str, int len)
{
int i;
for (i = len/2-1 ; i >= 0 ; --i) {
char c = str[i];
str[i] = str[len-i-1];
str[len-i-1] = c;
}
return str;
}
Optimizing for speed at that level, look at the inline keyword, also compile with (for gcc) with -O3
(does usually a better job that adding register ... by yourself).
优化该级别的速度,查看内联关键字,也可以使用-O3编译(对于gcc)(通常自己添加寄存器更好)。
If you need to have the reversed string elsewhere, either provide it in the function (being allocated for strlen(str)+1
- actually len+1
here - characters)
如果你需要在其他地方使用反向字符串,或者在函数中提供它(为strlen(str)+1分配 - 实际上是len + 1这里 - 字符)
char* str_reverse(char *str, char *reverse, int len)
{
int i;
for (i = len-1 ; i >= 0 ; --i) {
reverse[i] = str[len-i-1];
}
reverse[len] = 0;
return reverse;
}
or malloc it (it will have to be freed by the caller).
或malloc它(它必须由调用者释放)。
char* str_reverse_malloc(char *str, int len)
{
char *reverse = malloc(len+1);
if ( ! reverse) return NULL;
int i;
for (i = len-1 ; i >= 0 ; --i) {
reverse[i] = str[len-i-1];
}
reverse[len] = 0;
return reverse;
}
#3
3
The "most" optimized way must address the question of CPU and memory architecture as well as what is being reversed (long strings or short strings and what is the distribution).
“最”优化的方法必须解决CPU和内存架构以及正在逆转的问题(长字符串或短字符串以及分布是什么)。
There's no way to get relax the O(N) requirement, but one can use techniques such as loop unrolling, loop blocking and parallelism to optimize cache misses for very large strings. Also one can increase the word size and swap words, dwords or larger entities in-place (while dealing with the probable alignment issue).
没有办法放松O(N)要求,但可以使用诸如循环展开,循环阻塞和并行性等技术来优化非常大的字符串的缓存未命中。还可以增加字大小并就地交换字,双字或更大的实体(同时处理可能的对齐问题)。
// This will most likely be faster than byte-wise copying, but it's not O(N/8)...
//这很可能比逐字节复制更快,但它不是O(N / 8)......
if (len & 7 == 0)
{
uint32_t *dst = src+len-4;
uint32_t *src = (uint32_t *)ptr;
while (src<dst)
{
a = *src; b = *dst;
*src++ = byte_swap(b);
*dst-- = byte_swap(a);
}
}
#4
2
int main() {
char str[100], temp;
int i, j = 0;
printf("\nEnter the string :");
gets(str);
i = 0;
j = strlen(str) - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("\nReverse string is :%s", str);
return (0);
}
#5
0
Here is a variation that does not require the length to be passed and will swap both the beginning and ending characters at a given offset within the string each pass through the loop:
这是一个不需要传递长度的变体,它将在每个通过循环的字符串中的给定偏移处交换开始和结束字符:
/** strrevstr - reverse string, swaps src & dest each iteration.
* Takes valid string and reverses, original is not preserved.
* If str is valid, returns pointer to str, NULL otherwise.
*/
char *strrevstr (char *str)
{
if (!str) {
printf ("strrevstr() error: invalid string\n");
return NULL;
}
char *begin = str;
char *end = str + strlen (str) - 1;
char tmp;
while (end > begin)
{
tmp = *end;
*end-- = *begin;
*begin++ = tmp;
}
return str;
}
#6
-1
This is the fastest way to print a reverse string in c
这是在c中打印反向字符串的最快方法
#include <stdio.h>
#include <string.h>
int main()
{
char str[30],str1[30];
printf("Enter string :");
scanf("%s",str);
int i = strlen(str);
for (int j = 0; j <=5;j++)
{
str1[j]=str[i-j];
printf("%c",str1[j]);
}
printf("\n");
}