Output of given programming segment

Hey all,
I just found a problem which I don't understand at all.
Please explain to me what will be the output and how it is got.

Turbo C++

(Assuming all the required header files are included in the program):

char*NAME = "a ProFiLe";
for(int x=0;x<strlen(NAME);x++)
if(islower (NAME[x]))
NAME[X]= toupper (NAME[x]);
else
if(isupper (NAME[x]))
if(x%2!=0)
NAME[x]= tolower (NAME[x-1]);
else
NAME[x]--;
cout<<NAME<<endl;

P.S : I don't understand why there aren't any code blocks {} following the loop and the if statements but this is how it is given in my textbook.
Last edited on
It's not an error but it's pretty poor style, especially for a book. I hope this indentation helps you to understand the nested if statement.
1
2
3
4
5
6
7
8
9
10
11
char*NAME = "a ProFiLe";
for(int x=0;x<strlen(NAME);x++)
    if(islower (NAME[x]))
        NAME[X]= toupper (NAME[x]);
    else
        if(isupper (NAME[x]))
            if(x%2!=0)
                NAME[x]= tolower (NAME[x-1]);
            else
                NAME[x]--;
cout<<NAME<<endl;
First the ps: You don't need code blocks around a single statement, so the following:
1
2
3
4
5
6
7
8
9
 x = 3;
if( someCondition)
   doThis();
y = 4;
if( someOtherCondition)
{
   doThis();
   doThat(x, y);
}


if line 2 is true, it will only execute the next single statement, which is doThis();, the brackets on line 6 tell the if statement to do multiply statements if it evaluates to true.

Now, to explain your code: There are several functions, bool isLower(char) checks if a character is lower case, char toUpper(char) returns an upper case character, with the other 2 functions doing the opposite of that. With that, you should be able to figure out what the code does.
closed account (1yR4jE8b)
You don't need code blocks around a single statement


While you certainly don't need them, I don't recommend doing this. There is no harm in putting braces around a single line if, unless you're in some deranged competition where you win by having the least lines of code.

1
2
3
4
5
6
7
8
9
10
if(someCondition)
    doThis();
    y = 4;


if(someCondition)
{
    doThis();
    y = 4
}


The two if-statements look nearly identical thanks to incorrect identation, but behave differently thanks to the braces (or lack thereof). A novice may be hard-pressed to know *why* if they are looking at a snippet of code that is causing problems.
Thnx for all of your help.
Found that the output will be A OROoIiE.
For eg:
The letter P is stored in NAME[2]...
Since it is already upper , it is either converted into lowercase of the last character or it is decremented entirely.
Since x=2, x%2!=0 renders false and hence , the character gets decremented.
SO P becomes O.

The letter F is stored in NAME[5].
It is upper case and x%2!=0 renders true. Hence it is converted into the lowercase of the previous character which is o.

If the character is lowercase , it is simply converted to uppercase.

Spaces are printed out in the same way.

Agreed that the program is pretty poorly written.
Topic archived. No new replies allowed.