1

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.

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);
}
3
  • 2
    c tag is inappropriate Commented Aug 8, 2015 at 23:45
  • When you write code, you should test new functions as you add them. If you had tested ins as soon as you wrote it, you would have noticed the problem then and there. Commented Aug 9, 2015 at 0:07
  • Why aren't you using strings? Homework? Commented Aug 9, 2015 at 0:16

1 Answer 1

2

1) The assignment in the function ins() doesn't change the value at the caller:

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

You would need to use strcpy() to the copy the temp1 char array:

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):

cout<<T<<endl;

3) Since the insertion happens in the original array, you need to ensure the array is big enough. Do something like in main():

char T[256]="Good peo Good peo"; // 256 is some arbitrary size
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.