简体   繁体   English

字符串模式匹配和插入C ++

[英]String pattern matching and insertion C++

I am trying to match and insert a patern in string. 我正在尝试匹配并在字符串中插入模式。

Here in the Good peo Good peo and I am searching for peo and inserting ple . 在这里,在Good peo Good peo ,我正在寻找peo和插入ple

But the Output is coming this: 但是输出结果如下:

Good people Good peo /n
Good peo Good people

I need to have output to be like 我需要有输出才能像

Good people Good people

My code: 我的代码:

#include<stdio.h>
#include<stdlib.h>

#include<iostream>
#include<string>
#include<string.h>
using namespace std;

int length(char s[])
{
    int len=0;
    int i=0;
    while(s[i]!='\0')
    {
        i++;
        len++;
    }
    return len;
}

void concatenate(char s1[], char s2[])
{
    int i=length(s1);
    int j=length(s2);
    int count=0;
    while(count<=j)
    {
        s1[i]=s2[count];
        i++;
        count++;
    }
}

void substring(char s[], char dest[], int ip, int len)
{
    int i=ip;
    int count=0;
    while(count<len)
    {
        dest[count]=s[i];
        count++;
        i++;
    }
    dest[count]='\0';
}

void ins(char T[], int ip, char P[])
{
    char temp1[100];
    char temp2[100];
    substring(T, temp1, 0, ip);
    substring(T, temp2, ip, length(T)-ip);
    concatenate(temp1, P);
    concatenate(temp1, temp2);
    T=temp1;
    cout<<T<<endl;
}

void del(char T[], int ip, int L)
{
    char temp1[100];
    char temp2[100];
    substring(T, temp1, 0, ip);
    substring(T, temp2, ip+L, length(T)-ip-L);
    concatenate(temp1, temp2);
    T=temp1;
    cout<<T<<endl;
}

//where T is the original string and P is the pattern to be deleted whereever it appears in the original string.
void delpat(char T[], char P[])
{
    char temp[100];
    for(int i=0; i<=length(T); i++)
    {
        substring(T, temp, i, length(P));
        if(strcmp(temp, P)==0)
            del(T, i, length(P));
    }
}

//where T is the original string, Q is the pattern to be inserted and P is the pattern after which it is inserted.
void inspat(char T[], char P[], char S[])
{
    char temp[100];
    for(int i=0; i<=length(T); i++)
    {
        substring(T, temp, i, length(P));
        if(strcmp(temp, P)==0)
            ins(T, i+length(P), S);
    }
}

int main()
{ char a[100];
    char T[]="Good peo Good peo";
    char P[]="peo";
    char S[]="ple";
    inspat(T, P, S);
    gets(a);
}

1) The assignment in the function ins() doesn't change the value at the caller: 1)函数ins()中的赋值不会更改调用方的值:

T=temp1;
cout<<T<<endl;

You would need to use strcpy() to the copy the temp1 char array: 您将需要使用strcpy()复制temp1 char数组:

strcpy(T, temp1);
cout<<T<<endl;

2) Since you want to print the after inserting all occurrences, the above cout needs to go and you can print T either in main() or at the of inspat() (outside the for loop): 2)既然你想将所有出现后打印的,上面cout需要去,你可以打印T无论是在main()或在的inspat()外面for环):

cout<<T<<endl;

3) Since the insertion happens in the original array, you need to ensure the array is big enough. 3)由于插入发生在原始数组中,因此需要确保数组足够大。 Do something like in main() : main()做类似的事情:

char T[256]="Good peo Good peo"; // 256 is some arbitrary size

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM