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++) { ...

C++ Program to Reverse a String and Check Palindrome

C++ Program to Reverse a String and Check Palindrome

✅ C++ Program to Reverse a String and Check Whether It Is a Palindrome

#include <iostream>
#include <string.h>
using namespace std;

void rev(char str[]) {
    int start = 0, end = strlen(str) - 1;
    int temp;
    while (start < end) {
        temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        start++;
        end--;
    }
}

void pal(char str[]) {
    int start = 0, end = strlen(str) - 1;
    while (start < end) {
        if (str[start] != str[end]) {
            cout << "No. String is not palindrome:\n";
            return;
        }
        start++;
        end--;
    }
    cout << "Yes. String is palindrome:\n";
}

int main() {
    char str[100];
    cout << "Enter the string:\n";
    cin.getline(str, 100);
    cout << "Before reverse:\n";
    cout << str;
    cout << "\nAfter reverse:\n";
    rev(str);
    cout << str << "\n";
    pal(str);
}
  

๐Ÿ“˜ Explanation:

This program uses two user-defined functions:

  • rev() — reverses the given string manually by swapping characters from start and end.
  • pal() — checks whether the string is palindrome by comparing characters from both ends.

The program uses cin.getline() to take a string input (including spaces) and strlen() from the string.h library to find string length.

๐Ÿงพ Sample Output:

Enter the string:
level
Before reverse:
level
After reverse:
level
Yes. String is palindrome:
  

๐Ÿ”‘ Keywords:

C++ palindrome program, reverse string in C++, string manipulation in C++, C++ functions example, palindrome check, character swapping, string length

๐Ÿ“Œ Hashtags:

#CPlusPlus #String #Palindrome #ReverseString #Programming #CPPBasics

๐Ÿ” Search Description:

Learn how to reverse a string and check if it is a palindrome using functions in C++. Includes full explanation, sample output, and dark-themed code example.

Comments

Popular Posts

๐ŸŒ™