金陵科技学院实验报告
实验4 串
一、实验目的和要求
掌握串的存储及应用。
二、实验仪器和设备
Turbo C 2.0
三、实验内容与过程(含程序清单及流程图)
1、必做题
(1) 编写输出字符串s中值等于字符ch的第一个字符的函数,并用主函数
测试结果。
(2) 编写输出字符串s中值等于字符ch的所有字符的函数,并用主函数测
试结果。
解题思路:可以将第一题程序改进成一个子函数,在本题中循环调用。 (3) 设字符串采用单字符的链式存储结构,编程删除串s从位置i开始长
度为k的子串。
2、选做题
假设以链结构表示串,编写算法实现将串S插入到串T中某个字符之后,若串T中不存在这个字符,则将串S联接在串T的末尾。
提示:为提高程序的通用性,插入位置字符应设计为从键盘输入。 程序清单:
#include
void search(char a[],char ch) {
int i;
for(i=0;a[i]!='\\0';i++) {
if(ch==a[i]) { }
printf(\找到字符:%c 位置:%d\\n\exit(0);
金陵科技学院实验报告
}
}
printf(\没找到\\n\
main() { }
char a[maxsize],ch; printf(\请输入字符串s\\n\gets(a);
printf(\请输入要查找的字符ch\\n\scanf(\search(a,ch);
1.(2)
#include
void search(char a[],char ch) { }
int i,j=0;
for(i=0;a[i]!='\\0';i++) { } if(j==0)
printf(\未找到\if(ch==a[i]) { }
printf(\找到字符:%c 位置:%d\\n\j++;
金陵科技学院实验报告
main() { }
char a[maxsize],ch; printf(\请输入字符串s\\n\gets(a);
printf(\请输入要查找的字符ch\\n\scanf(\search(a,ch);
1.(3)
#include
char c;
struct chuanlian *next;
}chuan; chuan *s;
chuan *setup(chuan *s) {
chuan *head=NULL; chuan *rear=NULL; char ch;
printf(\请输入字符ch直到$\\n\); ch=getchar(); while(ch!='$') {
s->c=ch; if(head==NULL) { } else }
if(rear!=NULL)
rear->next=s; rear=s; head=s;
s=malloc(sizeof(chuan));
ch=getchar();
金陵科技学院实验报告
}
void delet(chuan *chu,int i,int k) { }
void display(chuan *chu) {
chuan *p;
p=chu; if(chu==NULL) {
printf(\空串\\n\); exit(0); int j; chuan *p; chuan *t; if(chu==NULL) { } p=chu;
for(j=1;j t=p->next; for(j=1;j p->next=t; if(p->next==NULL&&j t=t->next; printf(\串长度太小,无法删除%d个元素\\n\,k); exit(0); if(p->next==NULL&&j p=p->next; printf(\无法找到第%d位置\\n\,i); exit(0); printf(\空串无法删除\\n\); exit(0); rear->next=NULL; return head;
相关推荐: