开发者

While loop not working! [closed]

开发者_如何转开发 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
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.


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.


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);


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
...
...
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜