一道C语言真题

来源:百度知道 编辑:UC知道 时间:2024/05/26 11:10:48
1、编写函数char *insert(char *p),其功能是:在p指向的字符串中所有数字字符字串前插入一个符号'-'。函数返回p字符串的首地址。编写main函数,用给定的测试字符串初始化数组,调用函数insert对字符串做处理,将结果字符串输出到屏幕。
例如:测试数据为"AB1CD12EF123GH",则运行结果为:
"AB-1CD-12EF-123GH"
2、编写函数void SortNumber(int *a,int n,int m1,int m2)。其功能是:对a指向的数组中的n个元素按升序排序,再对其中第m1~m2个数做逆序处理。编写main函数,用给定的测试数据调用函数SortNumber,将处理后的结果在屏幕上输出。

1 memmov(p+1,p,strlen(p)+1); // +1是顺带着把字符串结尾的0也移动
*p = '-';
return p+1;

第一题:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define N 20

char *insert(char *p){
int i,j,n;
n=strlen(p);//n是字符串的长度
for(i=0;i<n;i++){
if(isdigit(*(p+i))){//找到数字
for(j=n+1;j>i;j--){//全部后移一个位置
*(p+j)=*(p+j-1);
}
*(p+i)='-';//插入'-'
n++;//字符串长度+1;
i++;//搜索位置跳过符号'-'
while(isdigit(*(p+i)))i++;//跳过连续的数字串
}
}
return p;
}

void main(){
char p[N]={"AB1CD12EF123GH"};
insert(p);
printf("%s\n",p);
}

第二题

#include <iostream>
using namespace std;

///采用插入排序
void Sort1( int *&a,int n ){////////////正序排列
int len = n;
int tmp;
for(int i = 1;i < len; ++i ){
tmp = a[i];int j;