Skip to main content

Featured

Merge Sort in C++

  Merge Sort in C++ Introduction Merge Sort is a popular sorting algorithm that follows the Divide and Conquer approach. It divides an array into smaller subarrays, recursively sorts those subarrays, and finally merges the sorted subarrays to produce a completely sorted array. In this tutorial, we will learn how to implement Merge Sort in C++ . The program divides the array into two halves using the mid index, recursively sorts both halves, and then combines them using the merge() function. Merge Sort has a time complexity of O(n log n) in the best, average, and worst cases. Table of Contents Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Keywords Conclusion Algorithm Start the program. Read the size of the array. Read the array elements from the user. Call the mer...

Recursive String Reversal in C

Recursive String Reversal in C

✅ Recursive String Reversal in C

#include <stdio.h>
#include <string.h>

// Recursive function to reverse the string
void reverseString(char str[], int start, int end) {
    if (start >= end)
        return;

    // Swap characters
    char temp = str[start];
    str[start] = str[end];
    str[end] = temp;

    // Recur for next pair
    reverseString(str, start + 1, end - 1);
}

int main() {
    char str[100];
    printf("Enter a string:\n");
    scanf(" %[^\n]", str);  // Read string with spaces

    printf("Original String: %s\n", str);
    
    reverseString(str, 0, strlen(str) - 1);

    printf("Reversed String: %s\n", str);

    return 0;
}
  

๐Ÿ“˜ Explanation:

This program demonstrates how to reverse a string using a recursive approach:

  • It defines a recursive function reverseString() that swaps characters from the beginning and end, moving toward the center.
  • Base case: if start >= end, the function returns.
  • Each recursive call handles the next inner pair of characters.
  • scanf(" %[^\n]", str) reads input including spaces.

๐Ÿงพ Sample Output:

Enter a string:
hello world
Original String: hello world
Reversed String: dlrow olleh
  

๐Ÿ”‘ Keywords:

Recursion in C, reverse string recursively, string functions in C, string reverse logic, string manipulation, reverse using function

๐Ÿ“Œ Hashtags:

#CProgramming #Recursion #StringReversal #BeginnerC #CodeWithC #StringLogic

Comments

Popular Posts

๐ŸŒ™