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
Where local variables are stored in c?
what are the advantages of a macro over a function?
Do array subscripts always start with zero?
What is static memory allocation? Explain
Is c is a high level language?
How do you define a string?
what is recursion in C
Write a c program to build a heap method using Pointer to function and pointer to structure ?
Explain what is the difference between null and nul?
Who invented bcpl language?
What is maximum size of array in c?
what is uses of .net
What are the different types of control structures in programming?
.main() { char *p = "hello world!"; p[0] = 'H'; printf("%s",p); }
Explain how do you determine whether to use a stream function or a low-level function?