Reverse the part of the number which is present from
position i to j. Print the new number.
eg:
num=789876
i=2
j=5
778986
Answer Posted / abdur rab
#include <stdio.h>
void reverse ( int* ip_array, int st_pos, int ed_pos )
{
if ( ( ip_array ) && ( st_pos < ed_pos ) ) {
* ( ip_array + st_pos ) ^= * ( ip_array +
ed_pos ) ^= * ( ip_array + st_pos ) ^= * ( ip_array +
ed_pos );
reverse ( ip_array, ++st_pos, --ed_pos );
}
}
int main ( int argc, char* argv [] )
{
int int_array [20];
int number = 789876;
int counter = 0;
int nloop = 0;
int start_pos = 2;
int end_pos = 5;
/* split the number into an array */
while ( number ) {
int_array [ counter++ ] = number % 10;
number = number / 10;
}
/* reverse the splited array */
reverse ( int_array, 0, counter - 1 );
/* reverse for the particular position */
if ( ( start_pos < end_pos ) && ( end_pos <=
counter ) ) {
reverse ( int_array, ( start_pos - 1 ), (
end_pos - 1 ) );
number = 0;
for ( nloop = 0; nloop < counter; nloop++ )
{
number *= 10;
number += int_array [ nloop ];
}
printf ( "\n %d", number );
}
return ( 0 );
}
| Is This Answer Correct ? | 2 Yes | 1 No |
Post New Answer View All Answers
What is time complexity c?
write a program fibonacci series and palindrome program in c
How do we open a binary file in Read/Write mode in C?
Explain what does the characters 'r' and 'w' mean when writing programs that will make use of files?
What is a protocol in c?
number of times a digit is present in a number
why use functions a) writing functions avoids rewriting the same code over and over b) using functions it becomes easier to write programs and keep track of what they are doing c) a & b d) none of the above
Explain how do you override a defined macro?
how we can make 3d venturing graphics on outer interface
Can you please compare array with pointer?
What is the use of f in c?
How do I use strcmp?
What is the correct code to have following output in c using nested for loop?
How pointer is different from array?
What is queue in c?