C-字符串逆序输出

Description

编写一个函数,功能是使输入的字符串逆序输出。

Input

输入一串字符串,注意字符串中不要有空格。
Output
输出该字符串的逆序。

Sample Input**

ABCDEFG

Sample Output

GFEDCBA

HINT

#include
#include
int main()
{
    char str[100];
    scanf("%s",str);
    int len;
    len=strlen(str);
    int fuction(char *, int);
    fuction(str,len);
    return 0;
}

参考解答:

#include
#include
int main()
{
    char str[100];
    scanf("%s",str);
    int len;
    len=strlen(str);
    int fuction(char *, int);
    fuction(str,len);
    return 0;
}

解1:用下标

int fuction(char *s, int n)
{
    int i;
    for(i=n-1;i>=0;i--)
        printf("%c",s[i]);
    printf("\n");
    return 1; //题目中并未提出对返回值的要求,随便输出1
}

解2:用指针

int fuction(char *s, int n)
{
    char *p;
    for(p=s+n-1;p>=s;p--)
        printf("%c",*p);
    printf("\n");
    return 1; //题目中并未提出对返回值的要求,随便输出1
}

你可能感兴趣的:(C-字符串逆序输出)