Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square πŸ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } πŸ“‹ Copy Code πŸ’» Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 πŸ”’ C Program to Print Standard Hollow Binary Row Square πŸ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

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

πŸŒ™