#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
    char a[100],b[50],c[50];
    int i,n,j,k,d;
    printf("enter the string: ");
    fgets(a,sizeof(a),stdin);

    printf("enter the string to be removed: ");
    fgets(b,sizeof(b),stdin);
    printf("enter the string to be searched: ");
    fgets(c,sizeof(c),stdin);
    for(i=0;a[i]!='\0';i++)

       {
            if(a[i]==b[0])

        {
            k=i;
            printf(" \n the first character is at %d",k);
            printf("\n lets see whelther searchstring is available");
            for(k=i,j=0;a[k]!='\0',b[j]!='\0';k++,j++)
            {
                if(a[k]==b[j])
                {
                    for(d=0;c[d]='\0';d++)
                    {
                        a[k]=c[d];
                    }

                }


            }
        }



       }
       for(i=0;a[i]!='\0';i++)
       {

         printf("%s",a[i]);
       }
       }

       i know there is lot of mistake here but i dont know where is logical mistake please modify my mistakes






































































































































}

Recommended Answers

All 2 Replies

Member Avatar for I_m_rude

please specify the mistakes. this is not a logical question you are asking. tell us the errors or problem which you are facing. ;)
Firstly , line 45, It should be %c not %s as you are printing character. Ans try to post your code after indentation.
thanks.

I don't particularly like how you're trying to lump everything together because it complicates an already complicated algorithm. There are three separate parts to replacing a substring:

  1. Find a matching substring.
  2. Make room for the replacement.
  3. Insert the replacement.

Separating the tasks conceptually will help you write code that can easily be modularized into functions. Also note that making room for the replacement isn't as simple as it first seems because you need to consider differing sizes of the matched and replacement substring:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char line[BUFSIZ] = "thisisatest";
    char match[BUFSIZ] = "isa";
    char replace[BUFSIZ] = "BOOGA";
    int i, j, k;

    /* Step 1: Find a matching substring (without the standard library) */
    for (i = 0; line[i]; ++i) {
        for (j = i, k = 0; line[j] && match[k]; ++j, ++k) {
            if (line[j] != match[k])
                break;
        }

        if (match[k] == '\0')
            break;
    }

    /* Step 2: Make room for the replacement (without the standard library) */
    if (line[i]) {
        int old_end = i + strlen(match);
        int new_end = i + strlen(replace);

        if (old_end > new_end) {
            /* Make a smaller hole */
            while ((line[new_end++] = line[old_end++]) != '\0')
                ;
        }
        else if (old_end < new_end) {
            /* Make a bigger hole (don't forget to shift the null character too) */
            int j = strlen(line);
            int k = strlen(line) + (new_end - old_end);
            int n = j - old_end + 1;

            while (n--)
                line[k--] = line[j--];
        }
    }

    /* Step 3: Insert the replacement (without the standard library) */
    if (line[i]) {
        for (j = 0; replace[j]; ++j)
            line[i++] = replace[j];
    }

    /* Print the result */
    printf(">%s<\n", line);

    return 0;
}

And here's an example of taking that code and modularizing it into functions. See how few changes were needed?

#include <stdio.h>
#include <string.h>

int find_match(const char *src, const char *match);
void make_hole_for(char *dst, const char *old_s, const char *new_s, int start);
void write_at(char *dst, const char *src, int pos);

int main(void)
{
    char line[BUFSIZ] = "thisisatest";
    char match[BUFSIZ] = "isa";
    char replace[BUFSIZ] = "BOOGA";

    int pos = find_match(line, match);

    if (line[pos]) {
        make_hole_for(line, match, replace, pos);
        write_at(line, replace, pos);
    }

    /* Print the result */
    printf(">%s<\n", line);

    return 0;
}

int find_match(const char *src, const char *match)
{
    int i, j, k;

    for (i = 0; src[i]; ++i) {
        for (j = i, k = 0; src[j] && match[k]; ++j, ++k) {
            if (src[j] != match[k])
                break;
        }

        if (match[k] == '\0')
            break;
    }

    return i;
}

void make_hole_for(char *dst, const char *old_s, const char *new_s, int start)
{
    int old_end = start + strlen(old_s);
    int new_end = start + strlen(new_s);

    if (old_end > new_end) {
        /* Make a smaller hole */
        while ((dst[new_end++] = dst[old_end++]) != '\0')
            ;
    }
    else if (old_end < new_end) {
        /* Make a bigger hole (don't forget to shift the null character too) */
        int j = strlen(dst);
        int k = strlen(dst) + (new_end - old_end);
        int n = j - old_end + 1;

        while (n--)
            dst[k--] = dst[j--];
    }
}

void write_at(char *dst, const char *src, int pos)
{
    int i = 0;

    for (i = 0; src[i]; ++i)
        dst[pos++] = src[i];
}

Finally, note that all error handling was omitted for brevity. Robust code would include checks to avoid buffer overflow and such.

commented: great +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.