简体   繁体   中英

Repeatedly removing and replacing the occurence of a substring from the input string

I have this homework question : Write a C program to find the new string after repeatedly removing the occurence of the substring foo from the input string using functions by repeatedly replacing each occurence of 'foo' by 'oof'.

This is my code:

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

void manipulate(char * a)
{
    int i, flag = 0;
    char newstr[100];

    for (i = 0; a[i] != '\0'; i++) {
        if (a[i] == 'f') {
            if ((a[i + 1] != '\0') && (a[i + 1] == 'o')) {
                if ((a[i + 2] != '\0') && (a[i + 2] == 'o')) {
                    i += 3;
                    flag++;
                }
            }
        }
        newstr[i] = a[i];
    }

    for (i = 0; i < flag; i++) {
        strcat(newstr, "oof");
    }

    printf("\nThe output string is %s", newstr);
}

int main()
{
    char a[100];

    printf("Enter the input string");
    scanf("%s",a);
    manipulate(a);

    return 0;
}

I think something is wrong with my code because the expected output is :

Enter the input string
akhfoooo
The output string is akhoooof

But my Actual output is :

Enter the input string
akhfoooo
The output string is akhoof

Could you please rectify the errors in my code?

Change this

scanf("%c",a);

to this

scanf("%s",a);

since you want to read a string, not a single characther.


Edit for your edit (see comments):

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

void manipulate(char * a)
{
    int i = 0;
    char *pch;
    size_t len = strlen(a);
    while(a[i]) // until we do not reach the null terminator
    {
        // so that we do not go after the end of the string
        if(i + 2 == len)
          break;
        // if we found 'foo'
        if(a[i] == 'f' && a[i + 1] == 'o' && a[i + 2])
        {
           // replace it with 'oof'
           a[i] = 'o';a[i + 1] = 'o'; a[i + 2] = 'f';
           i = i + 2; // and check for after the replacement we just made
        }
        // increment our counter to check the next charachter
        i = i + 1;
    }
    printf("\nThe output string is %s\n", a);
}

int main()
{
    char a[100];

    printf("Enter the input string");
    scanf("%s",a);
    manipulate(a);

    return 0;
}

If you want to use a function, strstr() would be nice.

This would work fine. Look out for border conditions and when to exit.

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

void manipulate(char * a)
{
    int i = 0;
    char *pch;
    size_t len = strlen(a);
    while(a[i]) // until we do not reach the null terminator
    {
    // if we found 'foo'
    if(a[i] == 'f' && a[i + 1] == 'o' && a[i + 2]=='o')
    {
    // replace it with 'oof'
    a[i] = 'o';a[i + 1] = 'o'; a[i + 2] = 'f';
    if ( i+3 == len){
    break;
    }
    else{

    i = i + 2; // and check for after the replacement we just made
    continue;
    }
    }
    else
{
    i = i+1;
}
    }
    printf("\n\n\nThe output string is %s \n", a);
    }

int main()
     {
     char a[100];

      printf("Enter the input string \n");
      scanf("%s",a);
      manipulate(a);

       return 0;
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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