Please explain this to me.

Find the output of the following program:
#include <iostream.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;
}

Output is #agaSbarr
Anyone can explain this to me.
I couldn't compile this for the following reasons:
1) there is no <iostream.h>, just <iostream>. Some old implementations used <iostream.h> which is just a copy of iostream without namespaces. This meant that you didn't need to write using namespace std. After fixing that my problem was:

2) ErrorL 'L': Undeclared identifier.

That was because you have a ; at the end of the first for block. That ends the loop immediately. I suppose you wanted {} around the second for loop.

3) When I can finally compile and run the code, I get this as my output. Is that what you actually expected or did you want an explanation of that?
aab#raSgr

The "working" code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

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;
  system("pause");
}


and the secret function in pseudo-code:

1
2
3
4
5
6
7
8
9
10
for each character in Str
{
  for each character in the first half of Str
  {
    if character is 'A' or 'E'
      replace with '#'
    else
      swap character with character at the opposite position in the string
  }
}


Going through the sudo-code, it almost looks like the loops aren't supposed to be nested, and you really just needed the loop to calculate L (length), but it goes out of scope when we exit the for loop. Here's another attempt:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

void Secret(char Str[ ])
{
  int L;
  for (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;
  system("pause");
}


Now I get #agaSbarr, and this is why:
1
2
3
4
5
6
7
8
calculate length of string
for each character in the first half of Str
{
  if character is 'A' or 'E'
    replace with '#'
  else
    swap character with character at the opposite position in the string
}


So as we iterate we get this:
C:0 Str: ArabSagar
C:1 Str: #rabSagar
C:2 Str: #aabSagrr
C:3 Str: #agbSaarr
C:4 Str: #agaSbarr

Last edited on
Thank you very much for detailed explanation.
it was very nicely explained...
Thanks a lot..
@stewbond

Can you please explain this to me

void main()
{
char a[]= “Exam-2011 AheAd”;
int i;
for(i=0; a[i] ]!= ‘\0’;i++)
{
if(a[i]>= 97 && a[i]<=122)
a[i] --;
else
if(a[i]>= ‘0’ && a[i]<= ‘9’)
a[i] = a[i -1];
else
if(a[i]>= ‘A’ && a[i]<= ‘Z’)
a[i]+ = 32;
else
a[i]= ‘#’;
}
puts(a);
}
That code with tags:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void main()
{
  char a[]= “Exam-2011 AheAd”;
  int i;
  for(i=0; a[i] ]!= ‘\0’;i++)
  {
    if(a[i]>= 97 && a[i]<=122)
      a[i] --;
    else
      if(a[i]>= ‘0’ && a[i]<= ‘9’)
        a[i] = a[i -1];
      else
        if(a[i]>= ‘A’ && a[i]<= ‘Z’)
          a[i]+ = 32;
        else
          a[i]= ‘#’;
  }
  puts(a);
}


For this one you really need to learn the else if construct.

If I fix a typo on line 5, assume that puts() is the same as printf(), use int main instead of void main, replace the “ characters with ", and format this with elseif, then I get:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
int main()
{
  char a[]= "Exam-2011 AheAd";
  int i;
  for(i=0; a[i] != '\0';i++)
  {
    if(a[i]>= 97 && a[i]<=122)
      a[i] --;
    else if(a[i]>= '0' && a[i]<= '9')
      a[i] = a[i -1];
    else if(a[i]>= 'A' && a[i]<= 'Z')
      a[i]+= 32;
    else
      a[i]= '#';
  }
  printf(a);
}


Also check this table for ascii equivalents:
http://www.asciitable.com/index/asciifull.gif

1
2
3
4
5
6
for each character in a
  if lowercase, subtract 1 (b becomes a, c becomes b, etc.)
  else if between 0 and 9, it becomes the previous character
  else if upper-case, make lowercase
  else turn it into a #
print a


Following these rules, your output is:
ew'l######agdac
E became e
x became w
a became `
m became l
- became #
2 became #
0 became #
1 became #
1 became #
  became #
A became a
h became g
e became d
A became a
d became c

Last edited on
@stewbond Thank you very much. You are great. so much hekpful . explains very well. God Bless you!
Sorry for disturbin again.. One more

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# include<iostream.h>
# include<ctype.h>
void change (char* state, int &s)
{ int b = s;
for (int x = 0; s>=0; x++, s−−)
if ((x+s)%2)
*(state+x) = toupper(*(state+b-x));
}
void main ( )
{
char s[ ] = “Punjab”;
int b = strlen (s)−1;
change (s, b);
cout<<s<<”#”<<b;
}


I am not getting output in my system
closed account (jiL16Up4)
@StewBond, you see, the above codes by @SumaMenon are for the old Borland Turbo C++ compiler. (And the OP must be somewhat related to the Indian Education Paradigm, like I am...). The visual C++ is a bit different.

Can you please help me a bit? I liked that pseudo code trick of yours. Can you please tell me in detail how exactly do you expand on that for further complex codes, like in file handling etc.? Any tricks or reference for that...
This is kind of fun.

First <iostream.h> should be <iostream>. Second, void main() should be int main(). Third, there are a bunch of strange copy+paste errors with non ascii characters that cause compiler errors. Fourth strlen is declared in string.h, I had to include that. Fifth cout needs std:: prepended.

Now the code looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <ctype.h>
#include <string.h>

void change (char* state, int &s)
{
    int b = s;
    for (int x = 0; s>=0; x++, s--)
        if ((x+s)%2)
            *(state+x) = toupper(*(state+b-x));
}

int main ( )
{
    char s[] = "Punjab";
    int b = strlen(s) - 1;
    change (s, b);
    std::cout<<s<<"#"<<b;
}


The change function can be interpreted like this:
1
2
3
4
for each character in the string
  The character is equal to the character at the opposite position (but uppercase)

Note that if there is an odd number of characters, nothing will happen.


The output is
BAJJAB#-1


Topic archived. No new replies allowed.