当前位置: 代码迷 >> C语言 >> 不等长字符串替换怎么做呀?
  详细解决方案

不等长字符串替换怎么做呀?

热度:800   发布时间:2008-04-08 16:31:34.0
不等长字符串替换怎么做呀?
不等长字符串替换怎么做呀?
void fun(char *str,char *t1,char *t2)
要求把串str中的所有t1替换成t2;注:t1,t2长度不一样。
搜索更多相关的解决方案: 串替  字符  

----------------解决方案--------------------------------------------------------
这是我做的
#include<stdio.h>
#include<string.h>
#define N  100
void replace(char *str,char *t1,char *t2)
{ int i,j,m1,m2,n,k,a,b,h;
  char temp[N];
  m1=strlen(str);
  m2=strlen(t2);
  for(i=0;i<m1;i++)
      for(j=i;j<m1;j++)
        {for(n=i,k=0;n<=j;k++,n++)temp[k]=str[n];
         temp[k++]='\0';
         if(strcmp(temp,t1)==0)
            { if(strlen(t1)>strlen(t2))
                {a=strlen(t1)-strlen(t2);
                 for(h=j+1;h<m1;h++)str[h-a]=str[h];
                 str[m1-a]='\0';
                 for(n=i,k=0;k<m2;k++,n++)str[n]=t2[k];
                }
              else
                {b=strlen(t2)-strlen(t1);
                for(h=m1-1;h>j;h--)str[h+b]=str[h];
                str[m1+b]='\0';
                for(n=i,k=0;k<m2;k++,n++)str[n]=t2[k];                
                }
            }
        }
}
void main()
{ char str[N],t1[N],t2[N];
  printf("输入字符串str:");
  gets(str);
  printf("输入字符串t1:");
  gets(t1);
  printf("输入字符串t2(t1,t2长度不一样):");
  gets(t2);
  replace(str,t1,t2);
  printf("字符串t2替换t1后字符串str为:");
  puts(str);
}
----------------解决方案--------------------------------------------------------
#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);
}
----------------解决方案--------------------------------------------------------
如果要替换多个同样的单词呢?感觉好像用链表还是要好些。上面的程序错哪了呢?
----------------解决方案--------------------------------------------------------
#include "stdio.h"
#define n 100
#include "string.h"

void main()
{
              
    char str1[n];
    int x;   int i,j;                              
    printf("input the string!\n");
    gets(str1);
    x=strlen(str1);          //字符串长度            
      for(i=0;i<x;i++)                 //外循环,直到倒数第3个字符为止(因为判断的是连续的3个字符)
    {
        if(str1[i]=='a')  //3个if判断是否遇到了a r e
        {
            if(str1[i+1]=='r')
            {
              if(str1[i+2]=='e')
              {
              str1[i]='b';        //遇到了则进行变换
              str1[i+1]='e';
              for(j=i+2;j<n;j++) // //将所需判断的数后面缩进
              {
                  str1[j]=str1[j+1];   
              }         
            }
              
            }
         
        }
        
    }
     
    printf("%s",str1);  
}

其实 只要查找到 替换  如果大就缩进  着是一个be替换are de  你参考下
----------------解决方案--------------------------------------------------------
我着也实现了 多个be 替换 are 希望对你门有帮助  有的时候好的思路很重要 清晰的思路能给解决题目节约一半的时间

[[it] 本帖最后由 yd4433 于 2008-4-12 18:35 编辑 [/it]]
----------------解决方案--------------------------------------------------------
  相关解决方案