【C编程】让字符串反向显示!
2020-05-09 本文已影响0人
逐风墨客
/*=========================================
* Copyright (c) 2020, 逐风墨客
* All rights reserved.
* 功能描述:输入任何字符创,并让字符串反向显示
* 所用函数:reverse_string()
* 返回值:字符串反向
=========================================*/
#define MAX_STRING_LENGTH 80
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *reverse_string(char *string);
char input_string[MAX_STRING_LENGTH];
char *output_string;
system("clear");
printf("\n *Please input the string: ");
gets(input_string);
output_string = reverse_string(input_string);
printf("\n *Result: %s\n", output_string);
return 0;
}
char *reverse_string(char *string)
{
int i, length;
char *strBuf;
length = strlen(string);
strBuf = (char *)malloc(length);
string = string + length;
for (i=0; i<length; i++)
{
*(strBuf + i) = *-string;
}
*(strBuf + i) = '\0';
return strBuf;
}