| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1315 人关注过本帖
标题:不等长字符串替换怎么做呀?
取消只看楼主 加入收藏
yqiong
Rank: 1
等 级:新手上路
帖 子:315
专家分:0
注 册:2007-7-4
结帖率:83.33%
收藏
 问题点数:0 回复次数:2 
不等长字符串替换怎么做呀?
不等长字符串替换怎么做呀?
void fun(char *str,char *t1,char *t2)
要求把串str中的所有t1替换成t2;注:t1,t2长度不一样。
搜索更多相关主题的帖子: 串替 字符 
2008-04-08 16:31
yqiong
Rank: 1
等 级:新手上路
帖 子:315
专家分:0
注 册:2007-7-4
收藏
得分:0 
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
struct strch{
    char ch;
    struct strch *next;
};


struct strch * creat()
{
    struct strch *head,*p1,*p2;
    int strl=0;
    int n=0;
    head=NULL;
    p1=p2=(struct strch*)malloc(sizeof(struct strch));
    printf("please enter the strch:");
    scanf("%c",&p1->ch);
    while((*p1).ch!='#')
    {
        n=n+1;
        if(head==NULL)head=p1;
        else
            p2->next=p1;
        p2=p1;
            p1=(struct strch*)malloc(sizeof(struct strch));
            scanf("%c",&p1->ch);
    }
    p2->next=NULL;
    return head;
}

void print(struct strch * head)
{
    struct strch *p1;
    p1=head;
    if(p1==NULL)
        printf("this is a null");
    else
        while(p1->next!=NULL)
        {
        printf("%c",(*p1).ch);
        p1=p1->next;
        }
}

void fun(struct strch *&head,struct strch *t1,struct strch *t2)//用t2替换链表(以head为头结点)中的t1
//[bo][size=5这个函数好像没起作用???[/size][/bo]
{
    struct strch *p1,*p;
    struct strch *headt1,*headt2;
    headt1=t1;headt2=t2;
    p1=head;
    int i=0;
    if(head==NULL) {printf("none exsit");exit(0);}
    else while(p1->next!=NULL)
    {
        if(p1->ch==t1->ch)
        {
            p=p1;
            for(;p1->ch==t1->ch;p1=p1->next,t1=t1->next);//循环判断head中是否有t1
            if(t1->next==NULL)//如果经上循环t1已经指向未,则说明head中有t1,并已用p标记了t1在head 中出现的起始
            {
                p->next=headt2;
                while(t2->next!=NULL)t2=t2->next;
                t2->next=p1;
                t2=headt2;//当链表中有多个t1时,要保证让t1,t2指向表头
                t1=headt1;
            }
        }
        else p1=p1->next;
    }
}

void main()
{
    struct strch *head,*t1,*t2;
    printf("产生长串");
    head=creat();
    printf("产生长串中的一个子串");
    t1=creat();
    printf("产生替代子串的另一个串");
    t2=creat();
    fun(head,t1,t2);
    print(head);
}
2008-04-09 11:19
yqiong
Rank: 1
等 级:新手上路
帖 子:315
专家分:0
注 册:2007-7-4
收藏
得分:0 
如果要替换多个同样的单词呢?感觉好像用链表还是要好些。上面的程序错哪了呢?
2008-04-12 18:00
快速回复:不等长字符串替换怎么做呀?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011917 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved