RandyMarsh
RandyMarsh

Reputation: 23

Odd output of string in C

I received an assignment to write a code that would erase the instances of a string in another string, and although my code does that successfully, the symbol ╠ appears many times at the end of the result string.

Example: For input string 1 - A string is a string, and an input string 2 - str The result should be A ing is a ing.

But I receive A ing is a ing╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠

Hoped I could get some assistance regarding this issue, cause no matter what I've tried I wasn't able to fix this.

#include <stdio.h>
#define STRING_SIZE 100

int StrippingFunc(char input_str1[STRING_SIZE], char input_str2[STRING_SIZE], char 
result_string[STRING_SIZE])
{
  if (input_str2[0] == '\n' || input_str2[0] == '\0')
  {
    return 0;
  }
  for (int k1 = 0; k1 < STRING_SIZE; k1++)
  {
    if (input_str1[k1] == '\n')
    {
        input_str1[k1] = '\0';
    }
  }
  for (int k2 = 0; k2 < STRING_SIZE; k2++)
  {
    if (input_str2[k2] == '\n')
    {
        input_str2[k2] = '\0';
    }
  }

  int Length;
  int length2 = 0;
  int index2 = 0;
  while (input_str2[index2] != '\0') // Loop used to determine input_string2's length.
  {
    length2++;
    index2++;
  } 
  int InString = 0;
  int i = 0;
  int j;
  int resultindex = 0;

  while (input_str1[i] != '\0')
  {
    Length = length2;
    int l = i;
    j = 0;
    int proceed = 1;
    if (input_str1[l] == input_str2[j])
    {
        while ((input_str2[j] != '\0') && (proceed != 0))
        {
            while (Length >= 0)
            {
                if (Length == 0)
                {
                    InString = 1;
                    i += (l-i-1);
                    proceed = 0;
                    Length = -1;
                }
                if (input_str1[l] == input_str2[j])
                {
                    Length--;
                    j++;
                    l++;
                }
                else if ((input_str1[l-1] == input_str2[j-1]) && (input_str2[j] == '\0'))
                {
                    proceed = 0;
                    Length = -1;

                }
                else
                {
                    proceed = 0;
                    Length = -1;
                    result_string[resultindex] = input_str1[l - 1];
                    resultindex++;
                }
            }
        }
    }
    else
    {
        result_string[resultindex] = input_str1[i];
        resultindex++;
    }
    i++;
  }
  return InString;
}

int main()
{
  char result_string[STRING_SIZE];
  char input_string1[STRING_SIZE];
  char input_string2[STRING_SIZE];

  printf("Please enter the main string..\n");
  // Your function call here..
  fgets(input_string1, STRING_SIZE + 1, stdin);
  printf("Please enter the pattern string to find..\n");
  // Your function call here..
  fgets(input_string2, STRING_SIZE + 1, stdin);
  int is_stripped = StrippingFunc(input_string1, input_string2, result_string);; // Your function call here..
  // Store the result in the result_string if it exists

  printf("> ");
  printf(is_stripped ? result_string : "Cannot find the pattern in the string!");
  return 0;
}

Upvotes: 1

Views: 82

Answers (1)

bruno
bruno

Reputation: 32596

But I receive A ing is a ing╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠

In the code after you fill result_string but you missed to add the final null character, because of that the printf after reach non initialized characters with an undefined behavior producing your unexpected writting. After

  while (input_str1[i] != '\0')
  {
     Length = length2;
     ...
  }

add

result_string[resultindex] = 0;

note you have the place for because result_string and input_str1 have the same size

Having

char input_string1[STRING_SIZE];
char input_string2[STRING_SIZE];

these two lines can have an undefined behavior :

fgets(input_string1, STRING_SIZE + 1, stdin);
fgets(input_string2, STRING_SIZE + 1, stdin);

because fgets may write after the end of the arrays, you need to remove +1 or to size the arrays one more

In

for (int k1 = 0; k1 < STRING_SIZE; k1++)
{
   if (input_str1[k1] == '\n')
   {
     input_str1[k1] = '\0';
   }
 }
 for (int k2 = 0; k2 < STRING_SIZE; k2++)
 {
   if (input_str2[k2] == '\n')
   {
     input_str2[k2] = '\0';
   }
 }

except if fgets fill all the arrays you have an undefined behavior working on non initialized characters because you do not stop when you reach newline or the null character.

In

int length2 = 0;
int index2 = 0;
while (input_str2[index2] != '\0') // Loop used to determine input_string2's length.
{
  length2++;
  index2++;
} 

length2 and length2 have exactly the same value, is it useless to have two variables, and in fact this lop is useless because the previous loop with the right termination already give you the expected length.

In

  printf(is_stripped ? result_string : "Cannot find the pattern in the string!");

I encourage you to replace printf by a puts not only to add a final newline to flush the output and make it more clear in case you start your program in a shell, but also because in case the input string contains for instance %d and it is not removed and is_stripped is true then printf will try to get an argument whose do not exist, with an undefined behavior

If you do all the corrections with your inputs your code will print > A ing is a ing without undefined behavior

Upvotes: 2

Related Questions