C语言编程,如何做交换?

来源:百度知道 编辑:UC知道 时间:2024/05/15 03:51:13
这个程序该如何把第一位数字和最后一位数字交换?
做到这里不会了,求教`

#include<stdio.h>
#define N 7
void main()

{
int i=0;
int num;
int a[N];
int count;
int h;
printf("请输入要加密的数据:\n");
scanf("%d",&num);
while(num!=0)
{
a[i]=num%10;
num=num/10;
i++;
}
count=i;
printf("加密后的数据是:\n");
for(i=0;i<count;i++)
{
a[i]=(a[i]+5)%10;
printf("%d",a[i]);
}
printf("\n");
}

#include<stdio.h>
#include<string.h>

#define N 7

void main()
{
int i=0;
int num;
char a[N],b[64];
int count;

printf("请输入要加密的数据:\n");
scanf("%s",a );
a[N-1]=0;

printf("加密后的数据是:\n");
memset( b,0,sizeof(b) );
conut = strlen[a];
for( i=0;i<count;i++ )
{
b[i] = a[count-i-1];
}
printf("%s\n",b);
}
用字符串比用int型好处理多了。

我写过一个倒写的脚本,输入123,输出3-2-1我用的是字符类型。
#include <stdlib.h>
#include <stdio.h>
struct node
{char *d;
struct node *link;
};
main()
{struct node *head,*p;
char c;
c=(char *)malloc(sizeof(char));
head=NULL;
while ((c=getchar())!='\n')
{p=(struct node *)malloc(sizeof(struct node));
p->d=c;
p->link=head;
head=p; 这三个句子要按从下到上的循序看就能看懂了,这是思路很好的绝句,领会它的思路,改动一下应该能满足你的要求。