0

Write and explain the output of following c++ code.

typedef char STRING[80]; void MIXITNOW(STRING S) { int Size=strlen(S); for(int I=0;I<Size–1;I+=2) { char WS=S[I]; S[I]=S[I+1]; S[I+1]=WS; } for(I=1;I<Size;I+=2) if(S[I]>=’M’ && S[I]<=’U’) S[I]=’@’; } void main() { STRING Word=”CRACKAJACK”; MIXITNOW(Word); cout<<Word<<endl; }

11th Sep 2017, 2:58 AM
Sonal Bajaj
Sonal Bajaj - avatar
2 Respostas
+ 3
hi, the above program swap the character like this CRACKAJACK RCCAAKAJKC then it will check the character whether it is greater than M and less than U. if it so then it will replace with @ symbol
11th Sep 2017, 5:23 PM
Nanda Balakrishnan
- 1
wonder why you use array of char instead single string ? string itself is array of char. use it per char by SubString. by the way index are : C is 0 R is 1 A is 2 C is 3 K is 4 A is 5 J is 6 A is 7 C is 8 K is 9 during loop i=0 to (9-1) , you stepped 2 per loop , means : 0 , 2 , 4 , 6 , 8 C A K J C are inside WS simultanously , in your S you shifted or swaped current 0 , 2 , 4 , 6 , 8 with 1 , 3 , 5 , 7 , 9 by puting WS in S. so it will be : C _ A _ K _ J _ C _ + R _ C _ A _ A _ K = R <-> C C <-> A A <-> K A <-> J K <-> C still CRACKAJACK , confusing , but well.. your conditional if(S[I]>=’M’ && S[I]<=’U’) S[I]=’@’; } never runs , because the position between 'M' and 'U' need to be declared or converted to ascii first then you can replace R with '@'.
11th Sep 2017, 11:13 PM
Mikhael Anthony
Mikhael Anthony - avatar