C程序字符串倒序输出

来源:百度知道 编辑:UC知道 时间:2024/09/21 15:39:28
输入一字符串,按相反顺序输出这段字符串。我编的这个程序差不多了,可以实现倒序输出,输出后又多了四个乱七八糟的符号。
请求各路高手帮我诊断下,谢谢
以下是程序:
/* Note:Your choice is C IDE */
#include "stdio.h"

char str(char a[20],char b[20])
{
int m=19,n;
for(n=0;m>-1;n++)
{
if(a[m]!='\0')
b[n]=a[m];
else b[n]=' ';
m--;
}
}
void main()
{
char a[20],b[20];
scanf("%s",a);
str(a,b);
printf("%s",b);
}

你的函数没有返回值。
把a,b两个数组设为全局变量就好了。
就这样就行了:
#include "stdio.h"
char a[20],b[20];
char str(char a[],char b[])
{
int m=19,n;
for(n=0;m>-1;n++)
{
if(a[m]!='\0')
b[n]=a[m];
else b[n]=' ';
m--;
}
}
void main()
{
scanf("%s",a);
str(a,b);
printf("%s\n",b);
}

str(char a[20],char b[20])没返回
{
int m=19,n;
for(n=0;m>=-1;n++)/*加=*/
{
if(a[m]!='\0')
b[n]=a[m];
else b[n]=' ';
m--;
}
}
tyyuu uuyyt %s 是从b地址开始‘\0’结束
你就没有结束符

for(i=0;i<20;i++)
printf("%c",&b[i]);

可以把你的str函数大改一下:
void str(char a[20],char b[20])
{
int m=strlen(a)-1,n;
for(n=0;m>-1;n++,m--)
b[n]=a[m];
b[n]='\0';
}

记得先要
#include<str