0
can someone please explain the output of the the following program:
*Important Note* all the desired header files are already included in the code. typedef char Str80[80] void main() { char *Notes Str80 Str="vR.zGood"; int L = 6; Notes = Str; while(L>=3) { Str[L]=isupper(Str[L])? tolower(str[L]): toupper(Str[L]); cout<<Notes<<endl; L--; Notes++; getch(); } }
4 Answers
+ 2
The body of while loop will be executed 4 times.
On each iteration variable L decreased by 1, Notes is incresed by 1.
statement
Str[L]=isupper(Str[L])?
tolower(Str[L]):
toupper(Str[L]);
reverses a case of the letter from string Str with index L.
Since L is changed from 6 to 3 the case of 4 charcters of the string "vR.zGood" will be reversed. After the loop the string will be equal to "vR.ZgOOd".
pointer Notes points to the beginig of the string "vR.zGood" before while-loop.
On each iteration of the loop string pointed by Notes is printed. Then Notes moves on to the next character of the string.
The output will be:
vR.zGoOd
R.zGOOd
.zgOOd
ZgOOd
or step by step:
L=6
a case of the character with index 6 is reversed (o->O).
Notes point to the 1st character of the string (v).
output: vR.zGoOd
L=5
a case of the character with index 5 is reversed (o->O).
Notes point to the 2nd character of the string (R).
output: R.zGOOd
L=4
a case of the character with index 4 is reversed (G->g).
Notes point to the 3rd character of the string (.)
output: .zgOOd
L=3
a case of the character with index 3 is reversed (z->Z).
Notes point to the 4th character of the string (Z).
output: ZgOOd
L=2
while loop ended
+ 2
There are many errors in your code.
Firstly, main should always return int. So it can't be-
void main()
Second, in line -
tolower(str[L]):
- there is no str defined. It should be Str.
Third, there is no getch() in C++. It is a C function.
Fix these errors, and then run the code.
+ 1
Run it yourself and see. You must have typed it wrong from the book. Also, unless you are using Turbo C++ compiler, getch() won't work. It's not compatible with modern C++. Use getchar() or cin.get() instead.
0
Thanks for the reply but actually this a question from my reference book so that's why i needed explanation for the resulting output. And i have asked the same question there is no error



