6
int main()  
{  
    clrscr();  
    char c[80],d[80];  
    cout<<"Enter a string = ";  
    cin.get(a,80);  
    strcpy(c,a);  
    strrev(a);  
    if(strcmp(c,a)==0)  
         cout<<"String = "<<c<< "is palindrome.";  
    else  
         cout<<c<<" is not palindrome";  
    getch();  
    return 0;
}

so there is any other way to accomplish this task in easy way without using array or in other way?

0

8 Answers 8

10
#include <string>
#include <algorithm>
#include <iostream>

int main()
{
    std::string s;
    getline(std::cin, s);

    if (std::equal(s.begin(), s.end(), s.rbegin()))
        std::cout << s << " is a palindrome\n";
    else
        std::cout << s << " is not a palindrome\n";
}

No arrays, no pointers.

Sign up to request clarification or add additional context in comments.

5 Comments

when m compiling this code there are aproximate 14-15 errors i got. I used the <iostream.h> with every liabrary and also without it but the error are not going off so where m I wrong can you explain sir.
@avirk - when you compile this exact program, what are the error messages you receive? Please copy-and-paste the precise messages - don't try to retype them or summarize them, just paste them into a response.
i don't know how to copy the eror message from C++ window will you tell me so i could post you. thanx
@avirk, which compiler are you using? Turbo C++ or BCC
here we should have to add one more libarary i.e #INCLUDE<ISTREAM> which hold the "getline" and then compile it and it will give you the desired result thanx sir for this nice answer.
9
bool is_palindrome(const char* s)
{
    const char *p = s;
    const char *q = s + strlen(s) - 1;
    while (p < q) {
        if (*p != *q)
            return false;
        ++p;
        --q;
    } 
    return true;
}

Comments

2

My solution: (not that effiecient though, but just yet another "different" solution)

bool is_palindrome(const std::string& s) 
{
   struct local
   {
        static bool work(const std::string& s, int l, int h) 
        {
           return l>= h? true: (s[l] == s[h]? work(s, l + 1, h -1): false);
        }
    };
    return local::work(s, 0, s.size() - 1);
}

//usage
cout << is_palindrome("liril"); //just pass one argument. that's it!

Demo : http://www.ideone.com/WNuEC

Comments

1

After c++11, variadic template brought new nice feature to solve this kind of problems. With c++17 solution was even better.

Here is my variadic template via folding expression for palindrome:

template <typename ...ARG>
bool isPalindrome(ARG ...args)
{
    std::string temp1 = "";
    ((temp1 += args), ...);

    std::string temp2 = "";
    ((temp2 = args  + temp2), ...);

    return temp1 == temp2;
}

int main(int argc, char *argv[])
{
    std::cout << isPalindrome('e','y', ' ', 'e', 'd','i','p',' ','a','d','a','n','a','d','a',' ','p','i','d','e',' ','y','e') << std::endl;
    return 0;
}

Comments

0

You asked about reversing a string in your title, but look like you're checking for palindromes?

There are a few collection classes that would work.

You could implement reversed access using a std:map. Store the letters as pairs of numerical indexes and single characters. When you create the map give it a sorting function to order them in reverse order by sorting backward on the letter's index.

If you actually need to change the order of the letters in the list then you could put those same pairs into a std:vector and resort them using the same trick but with two different comparison functions.

The most efficient way to do it is probably what you already have. The previous two methods have a lot of unnecessary overhead for this task.

Comments

0

You can use iterators, if you just want to check palindromes.

> #include <string> using std::string; using std::getline;
> 
> #include <iostream> using std::cin; using std::cout;
> 
> int main() {
>     string s;
>     getline(cin, s);
> 
>     string::reverse_iterator rit;
>     string::iterator it=s.begin();
>     for(rit=s.rbegin(); rit<s.rend(); rit++)
>     {
>        if(*rit==*it)
>           it++;
>        else
>        {
>           cout << s << " is not a palindrome\n";
>           exit(0);
>        }
> 
>     }
> 
>     cout << s << " is a palindrome\n";
>     exit(0);
> 
>   }

Comments

0

You can try this out:

int isPalin ( char *str ) {
    int i, len=strlen(str);
    for (i=0; i<len/2; ++i)
        if (str[i]!=str[len-i-1])
            break;
    return i==len/2;
}

This is pure C and quite efficient. Requires O(1) memory and O(n) time complexity.

Comments

0
public static void palindrome(string a)
        {
            bool y = true;
            for (int i = 0; i <= ((a.Length) / 2) && y; y = a[++i] == a[a.Length - (i + 1)]) ;
            Console.WriteLine(y ? "Palindrome" : "Not Palindrome");
        }

1 Comment

This does not look like C++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.