注册 登录
编程论坛 C++教室

字符串倒置.. 麻烦大神么看下

cifa 发布于 2013-11-01 00:00, 388 次点击
新人求教..  谢谢帮助
倒置字符串..
为什么结果什么都没有输出
...


#include <stdio.h>
#include <string.h>
#define N 5


void fun(char s[], char t[])
{
    int i=0;
   
    int n=strlen(s);

    while(i<=n)
    {
        t[i]=s[n-i];
        i++;        
    }
   

}

int main(void)
{

    char a[N],b[N];

    gets(a);
    fun(a,b);
    puts(b);
}
3 回复
#2
yss282013-11-01 00:25
改成这样,然后键入 abc
程序代码:
#include <stdio.h>
#include <string.h>
#define N 5


void fun(char s[], char t[])
{
    int i=0;
   
    int n=strlen(s);
    while(i < n)
    {
        t[i]=s[n-i-1];
        i++;        
    }
    t[i] = '\0';

}

int main(void)
{

    char a[N],b[N];

    gets_s(a);
    fun(a,b);
    puts(b);
}
#3
rjsp2013-11-01 08:41
关于 gets_s

C99 中将 gets 标注为 废弃的。(The gets function is obsolescent, and is deprecated.)
可以用 fgets 替代之。

C11 中加入一个可选的替代函数 char *gets_s(char *str, rsize_t n);
#4
cifa2013-11-01 09:13
额  谢谢..  我那个版本gets可以用..  貌似     发现问题了... 问题是最后一个字符时\0..我把它转置到最前面了..   谢谢
1