第十次上机任务

任务1:
输入一个字符串和一个正整数x,将该字符串中的后x个字符复制到另一个字符串y中,再对y串的内容前后倒置后存入数组z中并输出。
要求:用指针访问数组元素、用函数getx(char *c1)实现复制、用函数getr(char *c2)实现倒置。
我的程序:

#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("%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];  
}    
}  


截图:

--------------------分割线--------------------
任务2:
定义一维整形数组,对数组分别进行“由大到小”和"由小到大"排序并输出。
要求:用函数和指针实现排序

我的程序:

#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;}    


截图:

--------------------分割线--------------------
任务3:
输入字符串s,将字符放入d数组中,最后输出d中的字符串。
要求:
用函数和指针实现
我的程序:

#include<stdio.h>   
#include<string.h>   
#define N 20   
  
int main(void){  
     char a[N];  
     char *p=a;  
     gets(a);  
     while(*p!='\0')  
     {  
        if((*p>=48&&*p<=57))  
        strcpy(p,p+1);  
        else p++;  
     }  
       
     puts(a);  
 }  

 

截图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值