1

我的教授已经开始编写代码了,看起来……

#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++);内容,但除此之外,我很难过。

4

1 回答 1

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;
}
于 2013-01-25T16:18:21.323 回答