简体   繁体   English

为什么这个 for 循环无限运行?

[英]Why does this for loop run infinitely?

I was trying a problem, where I had to write "Accept" for a string ending with b.我正在尝试一个问题,我必须为以 b 结尾的字符串写“接受”。 I'm having trouble with this part of code:这部分代码有问题:

char s[100];
int length,i=0;
gets(s);
length=strlen(s);
for( i=0; i<length; i+2)
{
  if(s[i]=='b' && s[i+1]=='\0')
      printf("Accept");
  else printf("Not accept");
}

But this loop runs infinitely.但是这个循环无限运行。 The same problem is solved when I use this:当我使用这个时,同样的问题解决了:

while(s[i]!='\0')
{ 
   if(s[i]=='b' && s[i+1]=='\0')
      printf("Accept");
   else printf("Not accept");
   i+2;
}

I understand there is a problem in the logic of the for loop, & have tried a lot to figure it out, but failed.我知道 for 循环的逻辑存在问题,并且尝试了很多来解决它,但失败了。 I'm new to programming.Can you please help me with this.我是编程新手。你能帮我解决这个问题吗?

for( i=0; i<length; i+2)

i is not incremented. i没有增加。 So it should be:所以应该是:

for( i=0; i<length; i+=2)

You're not incrementing i in your for loop:您没有在for循环中增加i

for( i=0; i<length; i+2)

All this does is add 2 to i and throws away the result.所有这些都是将 2 添加到i并丢弃结果。 You were probably intending to increment by 2, but that won't work either.您可能打算增加 2,但这也行不通。 If your string contains an even number of characters, you'll never find the last character.如果您的字符串包含偶数个字符,您将永远找不到最后一个字符。

So what you need to do in increment by 1 on each iteration:因此,您需要在每次迭代中以 1 为增量执行的操作:

for( i=0; i<length; i++)

EDIT:编辑:

Better yet, just check the last character and get rid of the loop altogether:更好的是,只需检查最后一个字符并完全摆脱循环:

if (s[length-1] == 'b') {
    printf("Accept\n");
} else {
    printf("Not Accept\n");
}

Where do you initialize your string?你在哪里初始化你的字符串? And also are you missing a parentheses?而且你还缺少括号吗? Also you probably want to print "Not accept" once.你也可能想打印一次“不接受”。 Check below for solution.检查下面的解决方案。

But really it will be much easier if you directly check the last character of array for 'b' (using as index length-1 ), and you don't need the loop anymore (since you know the length).但实际上,如果您直接检查数组的最后一个字符是否为 'b'(用作索引length-1 )会容易得多,并且您不再需要循环(因为您知道长度)。

#include <stdio.h>
#include <string.h>
int main()
{
  char s[100];
  strcpy(s,"test");
  int length = 0, i = 0, j = 0;
  length=strlen(s);
  for( i=0; i<length; i++)
  {
    if(s[i]=='b' && s[i+1]=='\0')
    {
       printf("Accept");
       j = 1;
    }

  }
  if(j==0) printf("Not accept");
  return 0;
}

在您的for循环中,您从未增加i变量:

for( i=0; i<length; i+2) // should probably use i++ instead of i+2

The problem that you are trying to solve (if I am reading the attempts correctly) is that you want to establish if the last character is the letter 'b'.您试图解决的问题(如果我正确阅读了这些尝试)是您想确定最后一个字符是否是字母“b”。

You have found out the length of the string by using:您已使用以下方法找出字符串的长度:

length=strlen(s);

So using this fact you can do the following因此,使用这个事实,您可以执行以下操作

if (length > 0 && s[length-1] == 'b') {
   printf("Accept");
}
else
{
   printf("Not accept");
}

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

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