任务一:
#include<stdio.h>
#define N 100
void getx(char *c1);
void getr(char *c2);
void main()
{
char a[N];
puts("Enter a string:");
gets(a);
getx(a);
}
void getx(char *c1)
{
int n,k=0;int j=0;char temp[N];
puts("Enter an integer:");
scanf_s("%d",&n);
while(*(c1+j)!='\0')
{
j++;}
for(int i=j-n;i<j;i++)
{
temp[k]=*(c1+i);
k++;
}
temp[k]='\0';
getr(temp);
puts("The new string is ");
puts(temp);
}
void getr(char *c2)
{
int i=0;
char temp[N];
while(*(c2+i)!='\0')
{
i++;}
for(int j=0;j<i;j++)
{
temp[i-j-1]=*(c2+j);
}
for(int k=0;k<i;k++)
{
*(c2+k)=temp[k];
}
}
调试结果:
任务二:
#include<stdio.h>
#define N 100
int j(int*x,int n);
int s(int*x,int n);
void main()
{int a[N];
int n=0,i=0;
printf("请输入一组整数:\n");
while(1)
{scanf("%d",&a[i]);
i++;
n++;
if(getchar()=='\n')
break;}
j(a,n);
s(a,n);
}
int j(int*x,int n)
{int temp=NULL;
int i,j;
for(j=0;j<n;j++)
{
for(i=0;i<n-1;i++)
{if(*(x+i)<*(x+i+1))
{temp=*(x+i);
*(x+i)=*(x+i+1);
*(x+i+1)=temp;}
}
}
printf("由大到小:\n");
for(j=0;j<n;j++)
{printf("%d ",*(x+j));}
printf("\n");
return 0;}
int s(int*x,int n)
{int temp=NULL;
int i,j;
for(j=0;j<n;j++)
{
for(i=0;i<n-1;i++)
{if(*(x+i)>*(x+i+1))
{temp=*(x+i);
*(x+i)=*(x+i+1);
*(x+i+1)=temp;}
}
}
printf("由小到大:\n");
for(j=0;j<n;j++)
{printf("%d ",*(x+j));}
printf("\n");
return 0;}
任务三:
任务三:
#include<stdio.h>
#define num 100
void xx(char*p);
void main()
{
char a[num];
puts("输入字符串:");
gets(a);
xx(a);
}
void xx(char*p)
{
char b[num];
int i=0;
while(*p!='\0')
{
if(*p>=65&&*p<=90||*p>=97&&*p<=122)
{
b[i]=*p;
i++;
}
p++;
}
b[i]='\0';
puts(b);
}
调试结果: