design a function to reverse data in between two location in c

26 Sep 2022 Balmiki Mandal 0 C Programming

Reverse data between two locations

"Reverse data in between two locations" typically means to reverse the order of the data elements that lie between two given memory locations. For example, if you have an array of integers, you might want to reverse the order of the elements between indices 3 and 7.

To reverse the data in this case, you would swap the element at index 3 with the element at index 7, then swap the element at index 4 with the element at index 6, and so on, until you reach the middle of the range.

The result would be that the data elements between indices 3 and 7 have been reversed in order, while the rest of the array remains unchanged.

Note that the "two locations" could be any two memory locations, not necessarily contiguous. The term "location" refers to a specific memory address where data is stored.

C that reverses the data between two given locations in memory:

C programming

void reverse_data(char *start_ptr, char *end_ptr) {
    while (start_ptr < end_ptr) {
        char temp = *start_ptr;
        *start_ptr = *end_ptr;
        *end_ptr = temp;
        start_ptr++;
        end_ptr--;
    }
}

Here, start_ptr and end_ptr are pointers to the start and end locations of the data you want to reverse. The function works by swapping the values at start_ptr and end_ptr, then moving both pointers toward the center until they meet or cross over.

You can call this function with the appropriate pointers as arguments, like this:

int main() {
    char arr[] = "Hello, world!";
    int len = strlen(arr);
    reverse_data(&arr[2], &arr[9]);  // Reverse "llo, wo"
    printf("%s\n", arr);  // Output: "Hwo ,lleorld!"
    return 0;
}

In this example, the function is used to reverse the substring "llo, wo" within the string "Hello, world!". Note that the & the operator is used to pass pointers to the arr array elements, and that the len the variable is not used in this particular example.

 

Write a c programming  function to reverse data between two location

#include<stdio.h>
void my_strrev_1(char *,char *);
int main()
{
    char s[20],ch,*p;
    printf("Enter the string");
    scanf("%s",s);
    printf("Before s=%s\n",s);
    my_strrev_1(s+2,s+5);
    printf("After s=%s\n",s);
}
void my_strrev_1(char *p,char *q)
{
    char ch;
    while(p<q)
    {
        ch=*p;
        *p=*q;
        *q=ch;
        p++;
        q--;
    }
}

Output:

Enter the string: ELECTRO4U

Before string : ELECTRO4U

After string : ELRTCEO4U

 

Further Reading:

Concatenating Two Strings in C: A Step-by-Step Guide

Design a function to count given character in given string

Design a function to reverse the given string

Design a function to search given character in given string in c

Design a function to reverse the word in given string

Design a function replace the words in reverse order in a given string line

design a function to reverse data in between two location

Design a function to find string length in c

Design a Function to print the string character by character

Design a function to print the elements of a given integers array.

Design a function for bubble sort in c

Design a function to swapping of two number

Design a function to check the given number is Armstrong or not if Armstrong return 1 else 0

 

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [info@electro4u.net].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.