Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

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

๐ŸŒ™