Demo entry 6625396

string

   

Submitted by anonymous on Jun 20, 2017 at 16:35
Language: C++. Code size: 1.5 kB.

#include <stdio.h>
#define MaxSize 100
typedef struct
{
char str[MaxSize];
int length;
}String;
char Replace(String *S,int start,String T,String V)
{

    int i=start,j=0,k;
    while(i<S->length&&j<T.length)
{
 if(S->str[i]==T.str[j])
        {
            i++;
            j++;
        }
        else
        {
            i=i-j+1;
            j=0;
        }
    }
    if(j==T.length)
    {   k=i-T.length;
       if(T.length==V.length)
       for(i=0;i<V.length;i++)
       {
        S->str[k+i]=V.str[i];
       }
       else if(T.length>V.length)
       {
           for(i=0;i<V.length;i++)
          {
          S->str[k+i]=V.str[i];
          }
          for(i=k+T.length;i =S->length-1;i++)
          {
           S->str[i-(T.length-V.length)]=S->str[i];
          }
  S->length=S->length-(T.length-V.length);
       }
      else if(T.length<V.length)
 for(i=S->length-1;i>k+(V.length-T.length);i--)
S->str[i+(V.length-T.length)]=S->str[i];
for(i=0;i<V.length;i++)
       {
        S->str[k+i]=V.str[i];
       }
        S->length=S->length+V.length;
 return 1;
    }
    else
        return 0;
}
void main()
{
    String S={("chongqinyoudian"),13};
    String T={("chong"),5};
    String V={("beijing"),7};
    int i;
if(Replace(&S,0,T,V))
    {
        for(i=0;i<S.length;i++)
        printf("%c",S.str[i]);
    printf("\n");
    }
    else
        printf("主串S中不存在子串T\n");
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).