Skip to main content

Featured

C Program to Check Prime Number Using Efficient Logic

  Introduction A prime number is a number that has exactly two distinct positive divisors: 1 and itself. In this program, we check whether a given number is prime or not using a simple and efficient logic. This type of program is commonly used in mathematics, competitive programming, and basic algorithm learning for beginners in C programming. Problem Statement The task is to write a C program that determines whether a given integer is a prime number or not. The program takes a single integer input from the user and analyzes its divisibility. If the number has no divisors other than 1 and itself, it should be identified as a prime number; otherwise, it is not prime. This problem is important in number theory and has practical relevance in areas such as cryptography, data validation, and algorithm design.  Algorithm / Logic Explanation To check whether a number is prime, we need to verify that it is not divisible by any number other than 1 and itself. The algorithm follows a si...

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

๐ŸŒ™