C语言数组的题目,求解答
Problem Description输入一个不多于5位的正整数,要求:
(1)求出它是几位数;
(2)分别输出每一位数字;
(3)按逆序输出各位数字。
Input
输入一个不多于5位的正整数。
Output
输出数据有3行,第一行为正整数位数,第二行为各位数字,第三行为逆序的各位数字。
Sample Input
123
Sample Output
3
1 2 3
3 2 1
~
程序代码:
#include<stdio.h>
#include<string.h>
void fun ( void );
int main( void )
{
fun();
return 0;
}
void fun ( void )
{
#define __MAX 20
char s[2][__MAX];
char c;
size_t i;
memset(s,' ',sizeof (s));
for (i=0;(c=getchar())!='\n'&&i<__MAX/2;++i)
s[0][i*2]=s[1][__MAX-i*2-2]=c;
if (i==0)
{
puts("0");
return ;
}
s[0][i*2-1]=s[1][__MAX-1]='\0';
printf("%u\n",( unsigned )i);
puts(s[0]);
puts(s[1]+__MAX-i*2);
#undef __MAX
}

程序代码:#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char str[10]="";
int count = 0;
char temp;
printf("请输入数字,以\\0作为结束符!\n");
while((temp = getchar()) != '\n' && count < 10)
{
str[count++] = temp;
}
printf("这是一个 %d 位数\n", count);
printf("这个数的每一位分别是:\n");
int index = 0;
for (index = 0; index < count; index++)
{
printf("%c ",str[index]);
}
printf("\n逆序后的数为:\n");
for (int index = count - 1; index >= 0; index--)
{
printf("%c ",str[index]);
}
printf("\n");
return 0;
}