简体   繁体   English

C 中字符串的奇数 output

[英]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 .示例:对于输入字符串 1 - A string is a string ,并且输入字符串 2 - str结果应该是A ing is a ing

But I receive A ing is a ing╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠但是我收到Aing 是 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;
}

But I receive A ing is a ing╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠但是我收到 Aing 是 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.在填充result_string之后的代码中,但您错过了添加最终的 null 字符,因为printf在到达具有未定义行为的未初始化字符后会产生意外的写入。 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注意你有这个地方,因为result_stringinput_str1有相同的大小

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因为fgets可能会在 arrays 结束后写入,所以您需要删除+1或再调整 arrays 的大小

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.除非fgets填充了所有 arrays ,否则您对未初始化的字符有未定义的行为,因为您在到达换行符或 null 字符时不会停止。

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. length2length2具有完全相同的值,是否有两个变量没用,实际上这个 lop 是没用的,因为前一个具有正确终止的循环已经给了你预期的长度。

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我鼓励您用puts替换printf不仅要添加最后的换行符以刷新%d并使其更清楚,以防您在 Z2591C98B70119FE624898B1E424B5E91 中启动程序未删除且is_stripped为真,则printf将尝试获取不存在且行为未定义的参数

If you do all the corrections with your inputs your code will print > A ing is a ing without undefined behavior如果您对输入进行所有更正,您的代码将打印> A ing is a ing without undefined behavior

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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