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; ...

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

πŸŒ™