问题描述
我试图扭转在C字符串
下面是我
void reverse(char str[]) {
int i = 0;
int length;
// Get string length
for (i = 0; str[i] != '\0' ; ++i) {
length = i;
}
char reversed[1000];
int j;
j = 0;
// Reverse it
for (j = 0; j < length ; ++j) {
reversed[j] = str[length - j];
}
}
我知道颠倒
包含字符串逆转,但我不知道如何修改原始的 STR
没有丢弃的数据我需要的。
I know that reversed
contains the string reversed, but I'm not sure how to modify the original str
without throwing away data I need.
我也不知道如何设置 STR
到颠倒
不用循环的再次。
I also don't know how to set str
to reversed
without looping again.
难道是可以接受另做...
Would it be acceptable to do another...
int m;
m = 0;
for (m = 0; m < length ; ++m) {
str[j] = reversed[j];
}
通常我会说这许多的循环气味,但我也仍然与语言相当陌生,所以我不知道...
Usually I'd say this many loops smells, but I'm also still quite unfamiliar with the language so I'm not sure...
感谢所有答案的家伙,我AP preciate的编辑呢!
Thanks for all the answers guys, and I appreciate the edits too!
我结束了 ...
int main() {
char str[] = "Reverse me!";
int length;
for (length = 0; str[length] != '\0'; length++) {
}
printf("length => %d chars\n", length);
int j, k;
char c;
for (j = 0, k = length - 1; j < k; j++, k--) {
c = str[k];
str[k] = str[j];
str[j] = c;
}
printf("reversed => %s\n", str);
return 0;
}
有些事情我现在知道...
Some things I now know...
- 有一个
的strlen()
像PHP。但是,一直没有在书中还讨论了,再加上我需要熟悉空结尾的字符串。 - A
为
循环可以分配和做是用逗号分隔的多个事情。我从来不知道这个!
- There is a
strlen()
like in PHP. However, it has not been discussed in the book yet, plus I need to get familiar with null terminated strings. - A
for
loop can assign and do multiple things that are comma separated. I never knew this!
于是问是值得的:)
推荐答案
在您的code注释:
void reverse(char str[]) {
int i = 0;
int length;
// Get string length
for (i = 0; str[i] != '\0' ; ++i) {
length = i;
}
而不是复制我一定长度每一次你可以只等到年底。
Rather than copying the i to length every time you could just wait until the end.
size_t len = 0; // size_t is an unsigned integer that is large enough to hold the sizes
// of the biggest things you can have (32 bits on 32 bit computer,
// 64 bits on a 64 bit computer)
char * s = str;
while (*s) {
len++;
s++;
}
虽然编译器很可能是能够使这种优化你。
Though the compiler would probably be able to make this optimization for you.
您应该知道,虽然,有一个标准的字符串函数的strlen
(的#include&LT;文件string.h&GT;
),这将使用相同的一般算法测量一个char字符串的长度(查找结束),但通常是为目标处理器优化
You should know, though, that there is a standard string function strlen
( #include <string.h>
)which will measure the length of a char string using the same general algorithm (look for the end) but is usually optimized for the target processor.
len = strlen(str);
您的再次code:
Your code again:
char reversed[1000];
使用大阵列是很好的学习和简单的例子,但你也可以动态地根据你现在知道你需要的大小分配内存。这样做的标准功能是的malloc
这是文件stdlib.h
(也是的malloc .H
)。使用此函数分配的内存也应该被释放,虽然。
Using big arrays are good for learning and simple examples, but you can also allocate memory dynamically based on the size you now know you need. The standard function for doing this is malloc
which is in stdlib.h
(also in malloc.h
). Memory allocated with this function should also be freed, though.
int * p = malloc( 8 * sizeof(int) ); // allocate an array of 8 ints
/* ... work on p ... */
free(p);
/* ... don't access the memory pointed to by p anymore ... */
p = 0;
有也malloc系列等功能。有释放calloc
,其中分配内存并清除设置为0。还有一个名为的strdup
函数(ISN吨标准C,但在string.h中十分广泛使用),它接受一个字符串,并分配它的一个副本。这其实只是:
There are also other functions in the malloc family. There's calloc
, which allocates memory and clears sets it to 0. There is also a function called strdup
(which isn't in standard C, but is very widely available in string.h) which takes a string and allocates a duplicate of it. It is really just:
char * strdup(const char * str) {
size_t len = strlen(str);
char * s = malloc(len+1);
if (!s) {
return s;
}
return strcpy(s,str); // This could have been memcpy since you know the size
// and memcpy might have been faster on many processors
}
另一个有用的内存分配函数是的alloca
(而不是在C标准,可广泛提供类似的功能可与C99变长数组)。这是伟大的,但是从的malloc
的工作方式不同。直到当前函数返回,因为该存储器以相同的方式分配作为存储局部变量(从栈)分配内存,这只是可用
Another useful memory allocation function is alloca
(not in the C standard, but widely available and similar functionality is available with variable length arrays in C99). It is great, but works differently from malloc
. It allocates memory that is only useable until the current function returns because this memory is allocated in the same way as memory for local variables (from the stack).
更多的code的:
int j;
j = 0;
// Reverse it
for (j = 0; j < length ; ++j) {
reversed[j] = str[length - j];
}
在code:
void reverse_in_place(char * str, size_t len) {
size_t i, j;
for (i = 0, j = len - 1; i < j ; i++, j--) {
char a = str[i];
char z = str[j];
str[i] = z;
str[j] = a;
}
}
应该交换串的次序未做它的一个副本。对于奇数长度字符串它不会试图以自己交换的char中间
should swap the order of the string without making a copy of it. For strings with odd length it won't try to swap the middle char with itself.
这篇关于有助于在C倒车字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!