简体   繁体   English

永远不会达到其他条款

[英]Else clause never being reached

I seem to be having a problem writing a string search function. 我似乎在编写字符串搜索功能时遇到问题。 strlength,and strmid are both previously written functions that have been tested and are working. strlength和strmid都是以前编写的功能,已经过测试并且正在工作。

int strfind(char * string1, char * string2)
{
   /* if string1 contains the substring string2 returns
      the starting position of string2 in string1
      otherwise returns -1
      e.g.  strinc("hello world","wor") returns 6
            strinc("hello world","war") returns -1
   */
   int begPos = 0, endPos, count = 0, match = 1;
   char *tempStr;
   endPos = strlength(string2)-1;

   while (endPos <= strlength(string1)-1)
   {
        strmid(string1, begPos, endPos, tempStr);

        while (match == 1)
        {
             if (tempStr[count] == string2[count]) {
                 if (count < strlength(string2)) count++;
                 else break;
              }
              else match = 0;
        }

        if ( match == 1 ) return begPos;
        else { begPos++;
        endPos++; }
   }

   return -1;
}

The algorithm should be something like 算法应该是这样的

  • Get the segment of the string between begPos and endPos 获取begPos和endPos之间的字符串段
  • Compare this segment with string2 将此段与string2进行比较
  • If they're the same, increment the count and check the next array address 如果它们相同,则递增计数并检查下一个数组地址
  • If not, then the strings do not match and you haven't found the segment in your string yet and match becomes 0. 如果没有,那么字符串不匹配,你还没有在你的字符串中找到该段,匹配变为0。
  • If match not found, move the beginning and end position 1 array cell over. 如果未找到匹配,则移动开始和结束位置1阵列单元格。
  • If match = 1 then return begPos 如果match = 1则返回begPos
  • Repeat the 6 previous steps until endPos reaches the end of string1. 重复前面的6个步骤,直到endPos到达string1的末尾。
  • If endPos reaches the end of string1 without having found string2 within string1, return -1. 如果endPos到达string1的末尾而没有在string1中找到string2,则返回-1。

The problem I'm having is that 我遇到的问题是

while (match == 1)
{
    if (tempStr[count] == string2[count]) {
         if (count < strlength(string2)) count++;
         else break;
    }
    else match = 0;
}

never seems to reach the else clause. 似乎永远不会达到else条款。 The value returned always seems to be the value that begPos is initialized with. 返回的值似乎总是begPos初始化的值。 This is a homework piece, but I've rewritten it several times using different methods such as for loops, and done multiple dry runs and cannot seem to work out the problem. 这是一个家庭作业,但我已经使用不同的方法重写了几次,例如for循环,并做了多次干运行,似乎无法解决问题。 Any light you can shed would be greatly appreciated. 任何你可以流下的灯都会非常感激。

Cheers, 干杯,

espSquall espSquall

strmid function strmid功能

void strmid(char * string1, int start, int end, char * string2)
{
   /* copies the elements of string1 from start to end
      to string2  */

   int len, count2 = 0;

   for (len = start; len <= end; len++)
   {
       string2[count2] = string1[len];
       count2++;
   }

   string2[count2] = '\0';
}

I have only one point to raise. 我只有一点要提出来。

How do you think that tempStr is getting set to anything useful? 您认为tempStr如何设置为有用的东西?

The line char *tempStr; char *tempStr; sets it to whatever happened to be on the stack at that time while C, with its lack of "proper" pass by reference, cannot change it with the call: 将它设置为当时在堆栈上发生的任何事情,而C,缺少“正确”的引用传递,不能通过调用更改它:

strmid(string1, begPos, endPos, tempStr);

In order to change the pointer, you would have to pass in &tempStr rather than tempStr . 要更改指针,您必须传入&tempStr而不是tempStr

So, it appears to me that your tempStr is not pointing to anything usable. 所以,在我看来,你的tempStr并没有指向任何可用的东西。


And, based on your added strmid function, this program is definitely in the "undefined behaviour" class. 而且,根据你添加的strmid函数,这个程序肯定是在“未定义的行为”类中。 A quick fix, though kludgy, would be to change: 快速修复,虽然kludgy,将改变:

char *tempStr;

to: 至:

char tempStr[1000];

That might not fix all your problems (and it'll introduce a potential for buffer overflow) but it'll at least give you a well-defined program. 这可能无法解决您的所有问题(并且它会引入缓冲区溢出的可能性),但它至少会为您提供一个定义良好的程序。

2 points: 2分:

  • you need to reinitialie count to zero before the inner while 你需要reinitialie count内前为零while
  • you need to reinitialise match to 1 before the inner while 你需要重新初始化match内前1 while

If you don't do this, then if the first iteration doesn't match, match will never be 1 again, and count will have you checking memory you don't know the content of. 如果你不这样做,那么如果第一次迭代不匹配,匹配将永远不再是1,并且count将让你检查你不知道内容的内存。

This is in addition to the issue noted by paxdiablo. 这是paxdiablo提到的问题的补充。

Look at this code: 看看这段代码:

   if (tempStr[count] == string2[count]) {
       if (count < strlength(string2)) count++;
      else break; }
   else match = 0;

Can you see how match will never get set to zero? 你能看到匹配永远不会被设置为零吗?

if ( match == 1 ) return begPos; if(match == 1)return begPos; else { begPos++; else {begPos ++; endPos++; endPos ++; } }

match will always be 1 for it to get here 匹配将始终为1,它来到这里

Just re set match to 1 and count to zero at the start of the loop and allocate sufficient memory for tempStr* : 只需将匹配设置为1并在循环开始时计数为零,并为tempStr *分配足够的内存:

int endPos = strlen(string2)-1; int endPos = strlen(string2)-1; char* tempStr = (char*)malloc((size_t)(endPos+1)); char * tempStr =(char *)malloc((size_t)(endPos + 1));

while (endPos <= strlength(string1)-1) { count=0; while(endPos <= strlength(string1)-1){count = 0; match=1; 匹配= 1; ... ...

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

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