0

I made a code and my target is to put spacewhere the input word was found in a sentence. i neet to replece the small word with space

like:

Three witches watched three watches
tch

output:

Three wi es wa ed three wa es

I made this code:

#include<stdio.h>
#define S 8
#define B 50
void main() {
char small[S] = {"ol"};
char big[B] = {"my older gradmom see my older sister"};
int i = 0, j = 0;


for (i = 0; i < B; i++)
{
    for(j=0;j<S;j++)
    {
        if(small[j]!=big[i])
        {
            j=0;
            break;
        }
        if(small[j]=='\0')
        {
            while (i-(j-1)!=i)
            {
                i = i - j;
                big[i] = '\n';
                i++;
            }
        }
    }
}
puts(big);
}

3 Answers3

0

First of all, in your exemple you work with newline '\n' and not with space.
Consider this simple example:

#include<stdio.h>
#define S 8
#define B 50
void main() {
    char small[S] = {"ol"};
    char big[B] = {"my older gradmom see my older sister"};
    int i = 0, j = 0;
    int cpt = 0;
    int smallSize = 0; 

    // loop to retrieve smallSize
    for (i = 0; i < S; i++)
    {
        if (small[i] != '\0')
            smallSize++;
    }

    // main loop
    for (i = 0; i < B; i++)
    {
        // stop if we hit the end of the string
        if (big[i] == '\0')
            break;
        // increment the cpt and small index while the content of big and small are equal
        if (big[i] == small[j])
        {
            cpt++;
            j++;
        }
        // we didn't found the full small word
        else
        {
            j = 0;
            cpt = 0;
        }

        // test if we found the full word, if yes replace char in big by space
        if (cpt == smallSize)
        {
            for (int k = 0; k < smallSize; k++)
            {
                big[i-k] = ' ';
            }
            j = 0;
            cpt = 0;
        }   
    }

    puts(big);
}

You need first to retrieve the real size of the small array.
Once done, next step is to look inside "big" if there is the word small inside. If we find it, then replace all those char by spaces.

If you want to replace the whole small word with a single space, then you'll need to adapt this example !

I hope this help !

Yohboy
  • 264
  • 4
  • 13
0

A possible way is to use to pointers to the string, one for reading and one for writing. This will allow to replace an arbitrary number of chars (the ones from small) with a single space. And you do not really want to nest loops but une only one to process every char from big.

Last but not least, void main() should never be used except in stand alone environment (kernel or embedded development). Code could become:

#include <stdio.h>
#define S 8
#define B 50

int main() {                     // void main is deprecated...
char small[S] = {"ol"};
char big[B] = {"my older gradmom see my older sister"};
int i = 0, j = 0;
int k = 0;    // pointer to written back big

for (i = 0; i < B; i++)
{
    if (big[i] == 0) break;       // do not process beyond end of string
    if(small[j]!=big[i])
    {
        for(int l=0; l<j; l++) big[k++] = small[l];  // copy an eventual partial small
        big[k++] = big[i];    // copy the incoming character
        j=0;                  // reset pointer to small
        continue;
    }
    else if(small[++j] == 0)  // reached end of small
    {
        big[k++] = ' ';       // replace chars from small with a single space
        j = 0;                // reset pointer to small
    }
}
big[k] = '\0';
puts(big);
return 0;
}

or even better (no need for fixed sizes of strings):

#include <stdio.h>

int main() {                     // void main is deprecated...
char small[] = {"ol"};
char big[] = {"my older gradmom see my older sister"};
int i = 0, j = 0;
int k = 0;    // pointer to written back big

for (i = 0; i < sizeof(big); i++)
{
    if(small[j]!=big[i])
...
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

In C strings are terminated with a null character '\0'. Your code defines a somehow random number at the beginning (B and S) and iterates over that much characters instead of the exact number of characters, the strings actually contain. You can use the fact that the string is terminated by testing the content of the string in a while loop.

i = 0;
while (str[i]) {
  ...
  i = i + 1;
}

If you prefer for loops you can write it also as a for loop.

for (i = 0; str[i]; i++) {
  ...
}

Your code does not move the contents of the remaining string to the left. If you replace two characters ol with one character , you have to move the remaining characters to the left by one character. Otherwise you would have a hole in the string.

#include <stdio.h>

int main() {
  char small[] = "ol";
  char big[] = "my older gradmom see my older sister";
  int s; // index, which loops through the small string
  int b; // index, which loops through the big string
  int m; // index, which loops through the characters to be modified

  // The following loops through the big string up to the terminating
  // null character in the big string.
  b = 0;
  while (big[b]) {
    // The following loops through the small string up to the
    // terminating null character, if the character in the small
    // string matches the corresponding character in the big string.
    s = 0;
    while (small[s] && big[b+s] == small[s]) {
      // In case of a match, continue with the next character in the
      // small string.
      s = s + 1;
    }
    // If we are at the end of the small string, we found in the
    // big string.
    if (small[s] == '\0') {
      // Now we have to modify the big string.  The modification
      // starts at the current position in the big string.
      m = b;
      // First we have to put the space at the current position in the
      // big string.
      big[m] = ' ';
      // And next the rest of the big string has to be moved left. The
      // rest of the big string starts, where the match has ended.
      while (big[b+s]) {
        m = m + 1;
        big[m] = big[b+s];
        s = s + 1;
      }
      // Finally the big string has to be terminated by a null
      // character.
      big[m+1] = '\0';
    }
    // Continue at next character in big string.
    b = b + 1;
  }
  puts(big);
  return 0;
}
ceving
  • 21,900
  • 13
  • 104
  • 178