Array to array data – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Solved Problem! #CPP#2697 siteicon   siteicon   siteicon  

Problem Statement - Array to array data

Write output of the following code and explain its working.

TC++ #2697



Run Output


int a2a(char *a1,char *a2,int rev=1)
A function definition with character array reference of two strings and the third variable rev to mark a condition with default value as 1.
int i=0; char ch; //The declarations and initialisations
int n=strlen(a1); //Finding length of first array
This loop increments address of a1 (first character array) one by one and reads the value in character ch until it hits the null character. This is basically a loop traversing method using array address.
if(rev) a2[n-i-1]=ch;
If rev variable is set the loop will fill a2 (the second array as passed by pointer location) from the last location determined by n-i-1 and successively moved left.

else a2[i]=ch; 
If variable is not set it would be simple copy operation.

i++; This moves the loop to next index of a1.
return i; //This will basically return the number of times a1 has been traversed, which is basically length of both a1 and a2.

int main() {
char a[20]=”hello”;
char b[20]=””;
int c=a2a(a,b);
Since a2a is called without third parameter, it will set the default value of rev as 1 to the reverse of a will be copied to b.

This should print olleh5   which is array b which is reversed of array a followed by returned array length.

This will set third parameter as 0 which means normal array copy will happen from a to b.

This should print hello5 as b will now be just a copy of a and the figure 5 will be its length.

return 0;


  • This is a tricky program which can perform both string copy and string reversal quite easily.
  • This program traverses the loop by moving the address pointer location rather than by array position movement as usually done. Address incrementing method is usually more efficient in practical implementations.

Common Errors

  • Student need to take additional care in putting brackets in address increment line in the while comparison condition in the loop.


sunmitra| Created: 17-Jan-2018 | Updated: 17-Jan-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues