Given an array of characters, how would you reverse it? How
would you reverse it without using indexing in the array?

Answer Posted / abdur rab

#include <stdio.h>

void reverse ( char* cp_str )
{
char* cp_rev_ptr = NULL;
cp_rev_ptr = cp_str;
while ( ( cp_rev_ptr ) && ( *cp_rev_ptr != '\0' ) )
cp_rev_ptr++;
cp_rev_ptr--;

while ( cp_str < cp_rev_ptr ) {
*cp_str ^= *cp_rev_ptr ^= *cp_str ^=
*cp_rev_ptr;
cp_str++;
cp_rev_ptr--;
}
}

int main ( int argc, char* argv [] )
{
char array [] = {"dlroW olleH"};

printf ("\nBefore :%s", array );
reverse ( array );
printf ("\nAfter :%s", array );

return ( 0 );
}

Output
======
Before :dlroW olleH
After :Hello World

Is This Answer Correct ?    16 Yes 5 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain what is the difference between null and nul?

865


Which header file is essential for using strcmp function?

1161


What are the basic data types associated with c?

1017


Which header file is used for clrscr?

769


How pointer is different from array?

798


Explain what is operator promotion?

814


What is this pointer in c plus plus?

785


Write the test cases for checking a variable having value in range -10.0 to +10.0?

2045


Why is c still so popular?

780


Differentiate between a for loop and a while loop? What are it uses?

886


Between macros and functions,which is better to use and why?

1786


What are the advantages of the functions?

838


Why #include is used in c language?

769


What is 2 d array in c?

727


Explain the concept and use of type void.

837