We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,280 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

replacing a string in string

#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






































































































































}
3
Contributors
2
Replies
5 Hours
Discussion Span
9 Months Ago
Last Updated
3
Views
rithish
Posting Whiz in Training
268 posts since Apr 2011
Reputation Points: 23
Solved Threads: 9
Skill Endorsements: 0

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_m_rude
Deleted Member

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.

deceptikon
Challenge Accepted
Administrator
3,452 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0627 seconds using 2.74MB