Answers for "if a string is palindrome print unduplicated part"

C++
2

check a string is palindrome or not

#include <stdio.h>
#include <string.h>
int main()
{

     char str[80];
     int length;
     printf("nEnter a string to check if it's a palindrome: ");
     scanf("%s", str);     // string input
     length = strlen(str); // finding the string length
     int i, j, count;
     for (i = 0, j = length - 1, count = 0; i < length; i++, j--)
     {
          // matching string first character with the string last character
          if (str[i] == str[j])
          {
               count++; // if character match, increasing count by one.
          }
     }
     if (count == length) // if count == length, then the string is a palindrome.
     {
          printf("'%s' is a palindrome.n", str);
     }
     else // otherwise the string is not a palindrome.
     {
          printf("'%s' is not a palindrome.n", str);
     }
     return 0;
}
Posted by: Guest on July-04-2021
0

reads the string in then determines if the string is a palindrome.

#include <iostream>
using namespace std;
 
// Iterative function to check if given string is a palindrome or not
bool isPalindrome(string str)
{
    int low = 0;
    int high = str.length() - 1;
 
    while (low < high)
    {
        // if mismatch happens
        if (str[low] != str[high])
            return false;
 
        low++;
        high--;
    }
 
    return true;
}
 
int main()
{
    string str = "XYXYX";
 
    if (isPalindrome(str))
        cout << "Palindrome";
    else
        cout << "Not Palindrome";
 
    return 0;
}
Posted by: Guest on June-25-2020

Code answers related to "if a string is palindrome print unduplicated part"

Browse Popular Code Answers by Language