/*=========================================
* Copyright (c) 2020, 逐风墨客
* All rights reserved.
*
* 文件名称:study_revstring.c
* 运行环境:Linux操作系统
* 功能描述:输入任何字符串,并让字符串反向显示
=========================================*/
#define MAX_STRING_LENGTH 80
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *get_strrev(char str[]);
int main(void)
{
char strInput[MAX_STRING_LENGTH];
char *strOutput;
system("clear");
printf("\n *Please input the string: ");
scanf("%[^\n]", strInput); // 能输入除回车符的任何字符,包括空格符
strOutput = get_strrev(strInput);
printf("\n *Result: %s", strOutput);
return 0;
}
/*******************************************
* 函数介绍:char *get_strrev(char str[])
* 输入参数:无
* 输出参数:str-输入正串,输出反串
* 返回值:str-输出反串
*******************************************/
char *get_strrev(char str[])
{
int i, len;
char *tmp;
len = strlen(str);
tmp = (char *)malloc(len);
if (NULL == tmp)
{
printf("\n Error:malloc failure! \n");
exit(1);
}
str = str + len;
for (i=0; i<len; i++)
{
*(tmp + i) = *--str;
}
*(tmp + i) = '\0';
strcpy(str, tmp);
free(tmp);
tmp = NULL;
return str;
}
程序运行结果: