1.编写函数,输出给定的字符串s中从m字符开始后的r个字符(假设s中有m字符且后有r个字符)。
#include <stdio.h>
void getstr(char *str,int r) {
int i=0;
while(str[i]!='m')
i++;
for(int j=i+1;j<i+1+r;j++)
printf("%c",str[j]);
}
int main() {
char str[]="abcdefjmhigklwn";
getstr(str,3);
}
2.有n个整数放在数组a中,编写递归函数,求整数之和。
#include <stdio.h>
int sum(int *a,int n) {
if(n==1)
return a[0];
return sum(a,n-1)+a[n-1];
}
3.编写函数,对任意给定的int类型数组A,求最小元素值并输出。求最小元素用函数编写,以指针做参数。函数形式如下 int min_data(int *p, int n)
其中:指针p为数组首地址,n为数组元素个数
#include <stdio.h>
int min_data(int *p,int n){
int min=p[0];
for(int i=1;i<n;i++)
if(min>p[i])
min=p[i];
return min;
}
int main(){
int arr[]={4,2,3,45,6,7,2,2,5,6,1};
printf("%d",min_data(arr,11));
}
4.在D盘根目录下有文件source.txt,在该文件中存放某班学生的学号及成绩,每个学生的学号是唯一的,学号和成绩是整数,每个数据宽度为6位,编写程序求:
从键盘输入学生的学号,在文件中检索该学生:如找到,显示该学生的学号及成绩;不存在显示-1.
#include <stdio.h>
#include <stdlib.h>
void main() {
FILE *file;
int search,no,score;
if((file=fopen("D:\\source.txt","r"))==NULL) {
printf("open error");
exit(0);
}
scanf("%d",&search);
while(!feof(file)) {
fscanf(file,"%6d%6d",no,score);
if(no==search) {
printf("%6d%6d\n",no,score);
return;
}
}
printf("-1\n");
fclose(file);
}
5.已知如图所示一个链表,链表的每节是一个字符,要求:
1)根据给定的链表定义结点的结构体类型
2)编写函数删除链表中所有数字字符的结点
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char key;
struct node *next;
} node;
struct node *del(struct node *head) {
while(head->key>='0'&&head->key<='9')
head=head->next;
struct node *p=head;
while(p->next!=NULL)
if(p->next->key>='0'&&p->next->key<='9')
p->next=p->next->next;
else p=p->next;
return head;
}