我已经完成了以下函数来反转字符串:
char* reverseString(char *original_string)
{
int length = strlen(original_string);
char *end_of_string = original_string + (length - 1);
char *reversed_string = malloc(sizeof(char) * length);
int count = 0;
int top_limit = length;
while (count < length) {
reversed_string[count] = end_of_string[top_limit];
top_limit--;
count++;
}
return reversed_string;
}
我计划的策略是将指针放在原始字符串的末尾并向后复制到新字符串中。我不确定我在这里做错了什么,但是在做的时候:
char* prova = "hello";
char* reversed_string;
reversed_string = reverseString(prova);
printf("%d", strlen(reversed_string));
它没有显示正确的长度,并且在编译时我收到以下警告:
test.c:46: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘size_t’