while 循环不工作!

发布于 2024-11-03 01:03:10 字数 1293 浏览 2 评论 0原文

using namespace std;

void reverse(char a[],int slen, int elen)
 {
     int start,end,temp;
     for(start=slen,end=elen;start<end;start++,end--)
            {
                temp=a[start];
                a[start]=a[end];
                a[end]=temp;
            }
 }

 void reverseall(char b[])
  {
      char a[15];
      int i,j,n=0,len;
      for(i=14,j=0;i>=0,j<15;i--,j++)
           a[j]=b[i];

      i=0;
      j=0;

      while(a[i]!='\0')
         {cout<<"h";
            n++;
            i++;
         }

      while(j!=n)
         {
            if(j!=0)
              j++;
            len=0;
            while(a[j]!= ' ' && a[j]!='\0')
             {
                len++;
                j++;
             }
            reverse(a,j-len,j-1);
         }

     for(i=0;i<n;i++)
        cout<<a[i];
  }
int main()
 {
     char b[20]="hi how are you";
     reverseall(b);
     return 0;
 }

我修改了原地反转的工作程序,只添加了3行,而while循环现在不起作用。

 for(i=14,j=0;i>=0,j<15;i--,j++)
               a[j]=b[i];

          i=0;
          j=0;  

这是程序的链接,我希望输出为“you are how hi”。

谢谢。

using namespace std;

void reverse(char a[],int slen, int elen)
 {
     int start,end,temp;
     for(start=slen,end=elen;start<end;start++,end--)
            {
                temp=a[start];
                a[start]=a[end];
                a[end]=temp;
            }
 }

 void reverseall(char b[])
  {
      char a[15];
      int i,j,n=0,len;
      for(i=14,j=0;i>=0,j<15;i--,j++)
           a[j]=b[i];

      i=0;
      j=0;

      while(a[i]!='\0')
         {cout<<"h";
            n++;
            i++;
         }

      while(j!=n)
         {
            if(j!=0)
              j++;
            len=0;
            while(a[j]!= ' ' && a[j]!='\0')
             {
                len++;
                j++;
             }
            reverse(a,j-len,j-1);
         }

     for(i=0;i<n;i++)
        cout<<a[i];
  }
int main()
 {
     char b[20]="hi how are you";
     reverseall(b);
     return 0;
 }

I modified a working program of reversing in place, and just added 3 lines and the while loop is not working now.

 for(i=14,j=0;i>=0,j<15;i--,j++)
               a[j]=b[i];

          i=0;
          j=0;  

Here is the link to the program, I want the output as "you are how hi".

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花落人断肠 2024-11-10 01:03:10

条件 i>=0,j<15 可能不会按照您的想法进行操作。首先,计算i>=0(为真或为假),然后丢弃结果,然后j<15进行评估并给出结果。也就是说,i>=0,j<15 相当于 j<15。您可能想要的是 i>=0 && j<15

请注意我如何用二元与运算符替换逗号运算符。一般来说,表达式a, b表示先计算a,然后计算b,后者的结果是整个结果表达。除非评估a副作用(例如修改变量),否则这是无意义的。如果你提高警告级别,任何理智的编译器都会在这里警告你,你几乎应该总是这样做。

表达式a && b 表示首先评估a。如果为 false,则 a && 的结果为b 也是假的。否则,将计算 b,其结果是整个表达式的结果。也就是说,表达式 a &&仅当 ab 都计算为 true 时,b 才计算为 true。请注意,如果 a 已经为 false,则根本不会计算 b,因为无论 b 具有什么值,它都不可能影响结果不再了。这种行为称为短路评估

The condition i>=0,j<15 probably does not do what you think it does. First, i>=0 is evaluated (which is either true or false), then the result is discarded, and then j<15 is evaluated and delivers the result. That is, i>=0,j<15 is equivalent to just j<15. What you probably want is i>=0 && j<15.

Note how I replaced the comma operator with the binary-and operator. In general, the expression a, b means first evaluate a, then evaluate b, and the result of the latter is the result of the entire expression. Unless evaluating a has a side-effect (such as modifying a variable), this is nonsense. Any sane compiler will warn you here if you crank the warning levels up, which you should almost always do.

The expression a && b means first evaluate a. If it is false, the result of a && b is also false. Otherwise, b is evaluated, and the result of that is the result of the entire expression. That is, the expression a && b only evaluate to true if both a and b are evaluated to true. Note that b is not evaluated at all if a is already false, because no matter what value b had, it could not possibly influence the result anymore. This behavior is called short-circuit-evaluation.

卖梦商人 2024-11-10 01:03:10

将其更改为

  for(i=13,j=0;i>=0,j<15;i--,j++)
       a[j]=b[i];
  a[14] = '\0';

codepad)即可。

发生的情况是,您反转了输入字符串,但它末尾包含一个 \0 字符,因此您的数组的第一个字符是 \0 ,其余代码执行了什么都没有了。

所以我们现在要做的就是反转除 \0 字符之外的所有内容,并在字符串末尾添加 \0

请注意,您到处都在使用幻数。对于测试来说,这可能没问题,但您应该更改它,获取输入字符串的长度并使用它而不是幻数 13/14/15。

看一下这个键盘,看看如何做得更好。摘抄:

int b_len = strlen(b);
char* a = (char*)malloc((b_len + 1) * sizeof(char));
for(i = 0; i < b_len; i++)
     a[b_len - i - 1] = b[i];
a[b_len] = '\0';

[...]

free(a);

Change it to

  for(i=13,j=0;i>=0,j<15;i--,j++)
       a[j]=b[i];
  a[14] = '\0';

and it will work (codepad).

What happened was that you reversed the input string, but it contains a \0 character at the end, so your arrays first character was \0 and the rest of the code did nothing anymore.

So what we're doing now is to reverse everything except the \0 character and adding a \0 at the end of the string.

Please note that you're using magic numbers everywhere. For testing, this can be fine, but you should change this, f.e. getting the length of the input string and using this instead of the magic numbers 13/14/15.

Have a look at this codepad to see how it can be done better. Excerpt:

int b_len = strlen(b);
char* a = (char*)malloc((b_len + 1) * sizeof(char));
for(i = 0; i < b_len; i++)
     a[b_len - i - 1] = b[i];
a[b_len] = '\0';

[...]

free(a);
愁杀 2024-11-10 01:03:10

for循环不应该以“{”开头,以“}”结尾?只要它不止一根线。

for {
...
...
...
}

而不是:

for
...
...
...

The for loop should not have "{" in the start, and "}" in the end? As long as it is more than one line.

for {
...
...
...
}

instead of:

for
...
...
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文