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
namespace
s. 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 |