Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

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

πŸŒ™