A string s, a string t, determine whether t is a substring of s, if so, replace all occurrences of the t string in s with the specified character or string p, if not, output NO

enter

A string s, a string t, the character or string to be replaced

output

Output the modified s string

Input and output samples

Sample input #1

copy

asdfasdf
132
saf

Sample output #1

NO

#include<stdio.h>
#include<string.h>
int main()
{
    char s[100],t[100],p[100],a[100],b[100];
    gets(s);
    gets(t);
    gets(p);
    //输入
    int i,yz=0;
    for(i=0;t[i]!='\0';i++);
    int z;
    for(z=0;p[z]!='\0';z++);
        int n=0;
    for(int j=0;s[j]!=0;j++)
    {
        if(t[n]==s[j])
                n=n+1;
                else
                    n=0;//判断是否是字串
                if(n==(i-1))
                {
                     strcpy(a,s+(j+1)-(i-1));
                     strcpy(p+z,a+i);
                     strcpy(s+(j+1)-(i-1),p);//Delete the string and replace the string
                    yz++;
                    n=0;
                }
    }
                if(yz==0)
                    printf("No");
                    else
                        printf("%s",s);//output
    return 0;
}



 

Guess you like

Origin blog.csdn.net/sanxiaozhi_/article/details/127968269