Can someone make this as few lines of code possible and make it do the same thing?
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(void){
char word[81];
do{
bool palindrome=true;
cout << "Please enter a word" << endl;
cin>>word;
int length = strlen(word);
int(length/2);
if (length>0){
for(int i=0;i<(length);i++)
{
if(word[i]!=word[length-1-i])
palindrome=false;
}}
if(palindrome==true){
cout << "The word is a palindrome" << endl; }
else{
cout << "The word is not a palindrome" << endl;
}}
while (word != "END");
return(0) ;}
#include <iostream>
#include <cstring>
usingnamespace std;
int main(void){ char word[81]; do{ bool palindrome=true; cout << "Please enter a word" << endl; cin>>word; int length = strlen(word); int(length/2); if (length>0){ for(int i=0;i<(length);i++) { if(word[i]!=word[length-1-i]) palindrome=false; }} if(palindrome==true){ cout << "The word is a palindrome" << endl; } else{ cout << "The word is not a palindrome" << endl; }} while (word != "END"); return(0) ;}
Please use proper code formatting by editing your post and adding [code] and [/code] around your code.
Tell us, what do you expect this line to be doing? int(length/2);
Right now, it isn't doing anything. It's calculating length/2 (integer division) and then throwing away the result.
char word[81];
... while (word != "END");
word is a char array. You shouldn't use == on c-style strings (char arrays).
use while (strcmp(word, "END") != 0);
Edit: I misread OP, I thought he said to make it do the RIGHT thing, not the SAME thing. My post changes the logic.
I mean, I'm sure you mean something else but this practically allows the code to accomplish the same thing. condensed down to 14, with some simple logic and back spacing of code, aswell as some simplification on the cout << "\n"; part. It is possible to condense it into practically 1 line, but it would look extremely awful to read. NOTE: this is still completely your code except some simple byte saving, I'm sure there are easier ways to condense but this seems easy to read aswell as not looking painful to look at.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <fstream>
#include <cstring>
usingnamespace std;
int main(void) { char word[99];
do {bool palindrome = true;
cout << "Please enter a word\n";
cin >> word;
int length = strlen(word);
int(length / 2);
if (length > 0) { for (int i = 0;i < (length);i++) { if (word[i] != word[length - 1 - i])
{ palindrome = false; } } }
if (palindrome == true) { cout << "The word is a palindrome\n"; }
else { cout << "The word is not a palindrome\n"; } } while (word != "END"); return(0); }
If this isn't what you were looking for then please reply stating so.
the shortest it gets is (pseudocode.. )
if(string.reverse() == string) //don't recall the reverse function but its out there in the tools.
which I think is the above, but I believe you can condense it to one if statement if you don't mind it being fairly complex. It won't avoid the copy, but you can hide it in a temporary.
I could put it all on one line like the others but I don't think that is what you meant.
Besides there is no excuse for bad formatting. This is the same thing Thomas said but complete.
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
usingnamespace std;
int main()
{
string Word1;
cout << "Please enter a word" << endl;
cin >>Word1;
string Word2(Word1.rbegin(), Word1.rend());
if (Word1==Word2){cout << "Match" << endl;}
else{cout << "No Match" << endl;}
return(0);
}
that is what I was looking for on the temporary eliminate. Thanks, it was bugging me; I knew it was possible. Its down to 2 lines of actual work and 2 lines of user I/O now, with one of the work lines kind of riding along for 'free' as a variable declare. One more text line would go if you did std:: instead of using.
"no " is a 3-character const c-style string. So, "no " + (1 * 3) would be a pointer to the NULL character at index 3. And "no " + (0 * 3) would just be a pointer to the beginning of the string.
Thanks doug4, I didn't realize that was offsetting the address. I guess the earlier use of std::string made me think the string literal was an std::string. (Obviously it isn't). So then my second code-snippet was undefined behavior, ha.
I used a lambda because I wanted as few statements as possible in main(). I think just putting the declaration and input extraction on the same line would have been cheating (but inside the lambda is fine? Stupid...).
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
int main()
{
std::string word ;
while( std::cout << "enter a word (enter END to quit): " && std::cin >> word && word != "END" )
{
for( char& c : word ) c = std::tolower(c) ; // convert all upper case to lower case
// palindrome if chars from left to middle equal chars from right to middle
// https://en.cppreference.com/w/cpp/algorithm/equalconstbool palidrome = std::equal( word.begin(), word.begin()+word.size()/2, word.rbegin() ) ;
std::cout << "the word " << ( palidrome ? "is" : "is not" ) << " a palindrome\n" ;
}
}