pgm to reverse string using arrays i.e god is love becomes
love is god)
(assumption:only space is used for seperation of words)

no addtional memory used.i.e no temporary arrays can used.

Answer Posted / abdur rab

#include <stdio.h>

void reverse_string ( char* cp_string, int start, int end )
{
if ( cp_string && ( start < end ) ) {
*( cp_string + start ) ^= *( cp_string +
end )
^= *( cp_string + start ) ^= *(
cp_string + end );
reverse_string ( cp_string, ++start, --
end );
}
}


int main ( int argc, char* argv [] )
{
char ca_array [12]={"Hello World"};
char* cp_ptr = NULL;
char* cp_ptr_temp = NULL;

printf ( "\n Before Reverse :%s", ca_array );

reverse_string ( ca_array, 0, strlen ( ca_array ) -
1 );

cp_ptr_temp = cp_ptr = ca_array;
while ( NULL != ( cp_ptr_temp = (char*) strchr (
cp_ptr_temp, ' ' ) ) ) {
reverse_string ( cp_ptr, 0, ( cp_ptr_temp -
cp_ptr ) - 1 );
cp_ptr = ++cp_ptr_temp;
}
// change the final word
reverse_string ( cp_ptr, 0, ( ( strlen (
ca_array ) - 1 ) - ( cp_ptr - ca_array ) ) );

printf ( "\n After Reverse by Words :%s",
ca_array );
return ( 0 );
}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is there any algorithm to search a string in link list in the minimum time?(please do not suggest the usual method of traversing the link list)

1856


What are preprocessor directives?

664


Why can’t constant values be used to define an array’s initial size?

824


What is calloc malloc realloc in c?

583


shorting algorithmS

1792






Why is c so important?

589


What are the storage classes in C?

616


Can we use any name in place of argv and argc as command line arguments?

602


Explain what is a stream?

603


What is array of structure in c programming?

743


What functions are used in dynamic memory allocation in c?

584


How can you avoid including a header more than once?

553


Why main is used in c?

580


What are the rules for identifiers in c?

579


Is it better to use malloc() or calloc()?

641