那位大虾帮我用指针编一个字符串查找和替换集一身的函数。
要求丶用纯c.先查找丶后替换。编两个子函数即可。比如用键盘输入s1=bxbsbxbshfdjjsbx;s2=bx;这个可自己在函数里定义。s3[3]="xyz"输入的结果...
要求丶用纯c. 先查找丶 后替换。 编两个子函数即可。
比如用键盘输入s1=bxbsbxbshfdjjsbx; s2=bx; 这个可自己在函数里定义。s3[3]="xyz" 输入的结果为:s2在s1中出现了3次丶 然后输出替换后的结果为xyzbsxyzbshfdjjsxyz; 就这样 。 谢谢啦。。高分送上。 急等。
哎呀丶 不要用链表做丶 用指针做丶 可写这样写丶
一个main()函数。
一个字符串查找子函数cha()。
一个字符串替换函数ti();
谢谢啦。 展开
比如用键盘输入s1=bxbsbxbshfdjjsbx; s2=bx; 这个可自己在函数里定义。s3[3]="xyz" 输入的结果为:s2在s1中出现了3次丶 然后输出替换后的结果为xyzbsxyzbshfdjjsxyz; 就这样 。 谢谢啦。。高分送上。 急等。
哎呀丶 不要用链表做丶 用指针做丶 可写这样写丶
一个main()函数。
一个字符串查找子函数cha()。
一个字符串替换函数ti();
谢谢啦。 展开
2个回答
2012-03-01
展开全部
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int nDate;
struct node *pstnext;
}Node;
//链表输出
void output(Node *head)
{
Node *p = head->pstnext;
while(NULL != p)
{
printf("%d ", p->nDate);
p = p->pstnext;
}
printf("\r\n");
}
//链表建立
Node* creat()
{
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
{
printf("分配内存失败\r\n");
return NULL;
}
head->pstnext = NULL;
p = head;
while(cycle)
{
printf("请输入数据且当输入数据为0时结束输入\r\n");
scanf("%d", &Date);
if(0 != Date)
{
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
{
printf("分配内存失败\r\n");
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}
//链表按值查找
void research_Date(Node *head, int date)
{
Node *p;
int n=1;
p = head->pstnext;
while(NULL != p && date != p->nDate)
{
p = p->pstnext;
++n;
}
if(NULL == p)
{
printf("链表中没有找到该值");
}else if(date == p->nDate)
{
printf("要查找的值%d在链表中第%d个位置\r\n", date, n);
}
return;
}
//按序号查找
void research_Number(Node *head, int Num)
{
Node *p=head;
int i = 0;
while(NULL != p && i < Num)
{
p = p->pstnext;
i++;
}
if(p == NULL)
{
printf("查找位置不合法\r\n");
}else if(i == 0)
{
printf("查找位置为头结点\r\n");
}else if(i == Num)
{
printf("第%d个位置数据为%d\r\n", i, p->nDate);
}
}
//在指定元素之前插入新结点
void insert_1(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre && j < i-1)
{
pre = pre->pstnext;
j++;
}
if(NULL == pre || j > i-1)
{
printf("插入位置不存在\r\n");
}else
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配内存失败\r\n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}
}
//在指定元素之后插入新结点
void insert_2(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre->pstnext && j < i)
{
pre = pre->pstnext;
j++;
}
if(j == i)
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配内存失败\r\n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}else
{
printf("插入位置不存在\r\n");
}
}
//删除指定结点
void Delete_1(Node *head, int i3)
{
Node *p = head, *pre = NULL;
int j = 0;
while(NULL != p && j < i3)
{
pre = p;
p = p->pstnext;
j++;
}
if(NULL == p)
{
printf("删除位置不存在\r\n");
}else
{
pre->pstnext = p->pstnext;
free(p);
}
}
void main()
{
int date, num; //待查找数据
int i3; //指定删除元素的位置
int i1, i2, Newdate_1, Newdate_2; //待插入的新数据
Node *Head = NULL; //定义头结点
Node *Head_New = NULL;
//链表建立
Head = creat();
printf("输出建立的单链表\r\n");
output(Head);
//链表按值查找
printf("请输入待查找的数据\r\n");
scanf("%d", &date);
research_Date(Head, date);
//链表按序号查找
printf("请输入待查找序号\r\n");
scanf("%d", &num);
research_Number(Head, num);
//在指定第i1个元素之前插入新元素Newdate
printf("在指定第i个元素之前插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i1, &Newdate_1);
insert_1(Head, i1, Newdate_1);
printf("插入后新链表\r\n");
output(Head);
//在指定第i2个元素之后插入新元素Newdate
printf("在指定第i个元素之后插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i2, &Newdate_2);
insert_2(Head, i2, Newdate_2);
printf("插入后新链表\r\n");
output(Head);
//指定删除i3元素
printf("删除元素的位置\r\n");
scanf("%d", &i3);
Delete_1(Head, i3);
printf("删除后新链表\r\n");
output(Head);
return;
}
#include <stdlib.h>
typedef struct node
{
int nDate;
struct node *pstnext;
}Node;
//链表输出
void output(Node *head)
{
Node *p = head->pstnext;
while(NULL != p)
{
printf("%d ", p->nDate);
p = p->pstnext;
}
printf("\r\n");
}
//链表建立
Node* creat()
{
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
{
printf("分配内存失败\r\n");
return NULL;
}
head->pstnext = NULL;
p = head;
while(cycle)
{
printf("请输入数据且当输入数据为0时结束输入\r\n");
scanf("%d", &Date);
if(0 != Date)
{
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
{
printf("分配内存失败\r\n");
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}
//链表按值查找
void research_Date(Node *head, int date)
{
Node *p;
int n=1;
p = head->pstnext;
while(NULL != p && date != p->nDate)
{
p = p->pstnext;
++n;
}
if(NULL == p)
{
printf("链表中没有找到该值");
}else if(date == p->nDate)
{
printf("要查找的值%d在链表中第%d个位置\r\n", date, n);
}
return;
}
//按序号查找
void research_Number(Node *head, int Num)
{
Node *p=head;
int i = 0;
while(NULL != p && i < Num)
{
p = p->pstnext;
i++;
}
if(p == NULL)
{
printf("查找位置不合法\r\n");
}else if(i == 0)
{
printf("查找位置为头结点\r\n");
}else if(i == Num)
{
printf("第%d个位置数据为%d\r\n", i, p->nDate);
}
}
//在指定元素之前插入新结点
void insert_1(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre && j < i-1)
{
pre = pre->pstnext;
j++;
}
if(NULL == pre || j > i-1)
{
printf("插入位置不存在\r\n");
}else
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配内存失败\r\n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}
}
//在指定元素之后插入新结点
void insert_2(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre->pstnext && j < i)
{
pre = pre->pstnext;
j++;
}
if(j == i)
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配内存失败\r\n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}else
{
printf("插入位置不存在\r\n");
}
}
//删除指定结点
void Delete_1(Node *head, int i3)
{
Node *p = head, *pre = NULL;
int j = 0;
while(NULL != p && j < i3)
{
pre = p;
p = p->pstnext;
j++;
}
if(NULL == p)
{
printf("删除位置不存在\r\n");
}else
{
pre->pstnext = p->pstnext;
free(p);
}
}
void main()
{
int date, num; //待查找数据
int i3; //指定删除元素的位置
int i1, i2, Newdate_1, Newdate_2; //待插入的新数据
Node *Head = NULL; //定义头结点
Node *Head_New = NULL;
//链表建立
Head = creat();
printf("输出建立的单链表\r\n");
output(Head);
//链表按值查找
printf("请输入待查找的数据\r\n");
scanf("%d", &date);
research_Date(Head, date);
//链表按序号查找
printf("请输入待查找序号\r\n");
scanf("%d", &num);
research_Number(Head, num);
//在指定第i1个元素之前插入新元素Newdate
printf("在指定第i个元素之前插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i1, &Newdate_1);
insert_1(Head, i1, Newdate_1);
printf("插入后新链表\r\n");
output(Head);
//在指定第i2个元素之后插入新元素Newdate
printf("在指定第i个元素之后插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i2, &Newdate_2);
insert_2(Head, i2, Newdate_2);
printf("插入后新链表\r\n");
output(Head);
//指定删除i3元素
printf("删除元素的位置\r\n");
scanf("%d", &i3);
Delete_1(Head, i3);
printf("删除后新链表\r\n");
output(Head);
return;
}
追问
这位大哥丶咱能不能先不用链表呢丶 第一吧丶我属于菜鸟行的丶 链表这块还没多大接触呢丶
第二我现在最重要的是想知道用指针怎么做呢?能不能再给写一次呢丶? 谢谢昂、
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 100
int cha(char * a , char * b);
char * ti(char * a , char * b , char * c);
int main()
{
char * s1;
char * s2;
char * s3;
char * temp;
s1 = (char *)malloc (MAXLINE * sizeof(char));
s2 = (char *)malloc (MAXLINE * sizeof(char));
s3 = (char *)malloc (MAXLINE * sizeof(char));
scanf("%s",s1);
scanf("%s",s2);
scanf("%s",s3);
printf("一共出现了%d次\n",cha(s1 , s2));
printf("原字符串为:%s\n查找字符串为:%s\n替换字符串为:%s\n",s1,s2,s3);
temp = s1;
s1 = ti(s1 , s2 , s3);
printf("替换后字符串为:%s\n",s1);
free(s1);
free(s2);
free(s3);
free(temp);
return 0;
}
int cha(char * s1 , char * s2)
{
int counter = 0;
int i;
char * temp;
while(temp = strstr(s1,s2))
{
counter++;
s1 = temp + strlen(s2);
}
return counter;
}
char * ti(char * s1 , char * s2 , char *s3)
{
int counter = 0;
char * new_s; //新字符串指针
char * temp;
int offset = 0; //偏移量
int len_s1 , len_s2 , len_s3 , add_len; //各字符串的长度
int i; //循环变量,也用作s1的偏移量
counter = cha(s1 , s2);
len_s1 = strlen(s1);
len_s2 = strlen(s2);
len_s3 = strlen(s3);
add_len = (len_s3 - len_s2)*counter;
new_s = (char *)malloc((len_s1+add_len)*sizeof(char)+1);
temp = strstr(s1 , s2);
for(i=0;i < len_s1;)
{
if(s1+i == temp)
{
strncpy(new_s+offset , s3 , len_s3);
offset += len_s3;
i += len_s2;
temp = strstr(s1+i , s2);
}
else
{
*(new_s + offset) = *(s1+i);
offset++;
i++;
}
}
*(new_s + offset) = '\0';
return new_s;
}
#include <stdlib.h>
#include <string.h>
#define MAXLINE 100
int cha(char * a , char * b);
char * ti(char * a , char * b , char * c);
int main()
{
char * s1;
char * s2;
char * s3;
char * temp;
s1 = (char *)malloc (MAXLINE * sizeof(char));
s2 = (char *)malloc (MAXLINE * sizeof(char));
s3 = (char *)malloc (MAXLINE * sizeof(char));
scanf("%s",s1);
scanf("%s",s2);
scanf("%s",s3);
printf("一共出现了%d次\n",cha(s1 , s2));
printf("原字符串为:%s\n查找字符串为:%s\n替换字符串为:%s\n",s1,s2,s3);
temp = s1;
s1 = ti(s1 , s2 , s3);
printf("替换后字符串为:%s\n",s1);
free(s1);
free(s2);
free(s3);
free(temp);
return 0;
}
int cha(char * s1 , char * s2)
{
int counter = 0;
int i;
char * temp;
while(temp = strstr(s1,s2))
{
counter++;
s1 = temp + strlen(s2);
}
return counter;
}
char * ti(char * s1 , char * s2 , char *s3)
{
int counter = 0;
char * new_s; //新字符串指针
char * temp;
int offset = 0; //偏移量
int len_s1 , len_s2 , len_s3 , add_len; //各字符串的长度
int i; //循环变量,也用作s1的偏移量
counter = cha(s1 , s2);
len_s1 = strlen(s1);
len_s2 = strlen(s2);
len_s3 = strlen(s3);
add_len = (len_s3 - len_s2)*counter;
new_s = (char *)malloc((len_s1+add_len)*sizeof(char)+1);
temp = strstr(s1 , s2);
for(i=0;i < len_s1;)
{
if(s1+i == temp)
{
strncpy(new_s+offset , s3 , len_s3);
offset += len_s3;
i += len_s2;
temp = strstr(s1+i , s2);
}
else
{
*(new_s + offset) = *(s1+i);
offset++;
i++;
}
}
*(new_s + offset) = '\0';
return new_s;
}
更多追问追答
追问
能不能不用链表? 做呢丶 我就像看到两个子函数丶 谢谢。
追答
我的两个函数哪里有链表?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询