我的教授已经开始使用代码了,它看起来像......
#include "stdafx.h"
int main(int argc, char* argv[])
{
int numchar;
char mystring[] = "This is the string to be printed in reverse order";
numchar = reverseString(mystring);
puts("Reversed String is ");
puts(mystring);
}
int reverseString(char *mystring)
{
}
现在我应该完成它,那就是我被困住的地方。我查阅了无数的逆转字符串的示例程序,但所有这些都是以不同的方式完成的,我迷失在如何将其“转换”为我教授为我们为其规划的代码的适当上下文中。我也对'int argc'应该是什么感到困惑。但是,我想在计算并返回反转的字符数时,我可以简单地输入类似for(int length=0; str[length]!='\0';length++);
的内容,但除此之外,我很难过。
答案 0 :(得分:1)
如果您担心reverseString()函数将返回什么,那么您将反转整个字符串。因此,如果字符串的长度是偶数,则函数应返回length
,如果长度为奇数,则应返回length-1
除此之外,从你的问题来看,这是通常的字符串反转功能。
int reverseString(char *string)
{
int length, c;
char *begin, *end, temp;
length = string_length(string);
begin = string;
end = string;
end += length - 1;
while(begin<end){
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end--;
}
if(length % 2 == 0) return length;
else return length-1;
}
int string_length(char *pointer)
{
int c = 0;
while( *(pointer+c) != '\0' )
c++;
return c;
}