| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
 跟大牛学C++学算法数据结构

问题点数：0  回复次数：1

#include<stdio.h>
#include<stdlib.h>
//#define NULL 0
typedef struct node
{
char ch;
struct node *next;
}STRNODE;                      //串的链式存储结构表示

int str_len(STRNODE *s)            //串的长度计算函数/
{
return s->ch;
}

void str_input(STRNODE *s)          //串的输入函数/
{
int i=0;
char ch;
STRNODE *p,*q;
p=s;
q=s->next;
s->ch=0;
while((ch=getchar())!='\n')         //通过循环，从键盘输入一串字符串
{
q=(STRNODE *)malloc(sizeof(STRNODE));
q->ch=ch;
q->next=NULL;
p->next=q;
p=q;
s->ch++;
}
}
STRNODE *str_sub(STRNODE *sub,STRNODE *s,int pos,int len)
{//将串s第pos个字符开始的长度为len的字符序列复制到串sub中
int i=1;
STRNODE *p,*q;
p=s->next;
q=sub->next;

for(i=1;i<=pos;i++)
{
p=p->next;
}
for(i=1;i<=len;i++)
{q=(STRNODE *)malloc(sizeof(STRNODE));
q->ch=p->ch;
q=q->next;
p=p->next;
printf("0\n");
}
sub->ch=len;
return s;
return sub;

}
void str_print(STRNODE *s)
{//字符串输出显示
int i;
STRNODE *p;
p=s->next;
for(i=0;i<s->ch;i++)
{
putchar(p->ch);
p=p->next;
}
printf("\n");
}
{
printf("    串操作\n");
printf("\t4、求子串\n");
printf("\t5、退出\n");
}void main()
{
int position,length,no;
STRNODE str,str1,str2;
str.ch=0;
str1.ch=0,str2.ch=0;
while(1)
{
printf("    请输入你的选择：");
scanf("%d",&no);
switch(no)
{
case 4:printf("请输入一串字符：");
fflush(stdin);
str_input(&str1);
printf("请输入取子串的位置和长度：");
scanf("%d%d",&position,&length);
str_sub(&str,&str1,position,length);
printf("子串为：");
str_print(&str1);
str_print(&str);
system("pause");
system("cls");
break;
case 5:
return;
}
}
}

得分:0

char *sub(int start, int length) {
char *substring = (char *)malloc(sizeof(char) * (length - start));
int count = 0;
node *n = s;
while(count != start) {
count++;
if(count = start) {
break;
}
n = s->next;
}
for(int i = 1, j = 0; i <= length; i++, j++) {
substring[j] = n->ch;
}
return substring;
}
• 2
• 1/1页
• 1

Powered by Discuz, Processed in 0.043875 second(s), 8 queries.