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
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)
What are preprocessor directives?
Why can’t constant values be used to define an array’s initial size?
What is calloc malloc realloc in c?
shorting algorithmS
Why is c so important?
What are the storage classes in C?
Can we use any name in place of argv and argc as command line arguments?
Explain what is a stream?
What is array of structure in c programming?
What functions are used in dynamic memory allocation in c?
How can you avoid including a header more than once?
Why main is used in c?
What are the rules for identifiers in c?
Is it better to use malloc() or calloc()?