:
void reverse(char str[]) {
int i = 0;
int length;
for (i = 0; str[i] != '\0' ; ++i) {
length = i;
}
, .
size_t len = 0;
char * s = str;
while (*s) {
len++;
s++;
}
, , .
, strlen (#include <string.h>), char, ( ), .
len = strlen(str);
:
char reversed[1000];
, , . - malloc, stdlib.h ( malloc.h). , , .
int * p = malloc( 8 * sizeof(int) );
free(p);
p = 0;
malloc . calloc, , 0. strdup ( C, string.h), . :
char * strdup(const char * str) {
size_t len = strlen(str);
char * s = malloc(len+1);
if (!s) {
return s;
}
return strcpy(s,str);
}
- alloca ( C, C99). , , malloc. , , , , ( ).
:
int j;
j = 0;
// Reverse it
for (j = 0; j < length ; ++j) {
reversed[j] = str[length - j];
}
:
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 .