Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

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

๐ŸŒ™