how we can find the output on copy

Feb 3, 2016 at 8:11am
i want to find the output of this code on copy but while i am doing i am unable to find and also i am using watch function but for this code that is also not working
kindly help me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include<iostream.h>
#include<conio.h>
void secret(char str[])
{
for(int l=0;str[l]!='\0';l++)
for(int c=0;c<l/2;c++)
if((str[c]=='A')||(str[c]=='E'))
 str[c] = '#';
else
{
char temp=str[c];
str[c]=str[l-c-1];
str[l-c-1]=temp;
}
}
void main()
{
char Message[]="ArabSagar";
secret(Message);
cout<<Message<<endl;
getch();
}
Feb 3, 2016 at 8:57am
You need to fix the indentation to see what the code is doing.

You just need to work thru it one step at a time. There's no logic to understand, just follow the steps as the computer would. When I was studying, it was called a "dry run", and we'd be expected (in an exam) to do it on paper.
Feb 3, 2016 at 11:35am
yes for the same thing i am also try
Feb 3, 2016 at 11:53am
I'm not clear on what you're asking. Can you please explain what it is you don't understand.
Feb 4, 2016 at 10:42am
The first thing to do is to use proper indentation and add a bit of whitespace to make the code more legible.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream.h>
#include <conio.h>

void secret(char str[])
{
    for (int l = 0;  str[l] != '\0';  l++)
        for (int c = 0;  c < l/2;  c++)
            if ( (str[c] == 'A')  ||  (str[c] == 'E') )
                str[c] = '#';
            else
            {
                char temp  = str[c];
                str[c]     = str[l-c-1];
                str[l-c-1] = temp;
            }
}

void main()
{
    char Message[] = "ArabSagar";
    secret(Message);
    cout << Message << endl;
    getch();
}


Now at a glance I see a few non-standard things in the code, I guess it was written for Turbo C++. For now I'll let that pass.

I'd also suggest putting extra braces around the for loops and the if statement. The compiler doesn't care about them, but it may help a human reader to see what is the scope of the control statements.

One more thing I would do. Get rid of the confusing and hard-to-read variable name l. Again, the compiler doesn't care, but to a human reader it can be confusing to see expressions like l-c-1 and l/2

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void secret(char str[])
{
    for (int count = 0;  str[count] != '\0';  count++)
    {
        for (int c = 0;  c < count/2;  c++)
        {
            if ( (str[c] == 'A')  ||  (str[c] == 'E') )
            {
                str[c] = '#';
            }
            else
            {
                char temp      = str[c];
                str[c]         = str[count-c-1];
                str[count-c-1] = temp;
            }
        }
    }
}


For now, that's as far as I can go. Without a more specific question there is little more to be said.

Feb 5, 2016 at 7:13am
Thanks
Topic archived. No new replies allowed.