Condense

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) ;}
here you are
1
2
3
4
#include <iostream>
#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) ;}

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.
Last edited on
With apologies to @lastChance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

bool isPalindrome( string& V, int L, int R )
{
   return L >= R || ( V[L] == V[R] && isPalindrome( V, L + 1, R - 1) );
}

int main()
{
    string input;
    do 
    {
      cout << "Please enter a word:\n";
      cin >> input;
      cout << boolalpha << isPalindrome( input, 0, input.size() - 1 ) << '\n';
    }
    while (input != "End");
}
Last edited on
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>
using namespace 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.
Or there's this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    string input, inputRev;
    do 
    {
      cout << "Please enter a word:\n";
      cin >> input;
      inputRev = input;
      reverse(inputRev.begin(), inputRev.end());
      if (input == inputRev)  {cout << "Palindrome\n";}
      else {cout << "Not Palindrome\n";}
    }
    while (input != "End");
}


What is the actual requirement? What does "as few lines of code possible" really mean?
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.
Last edited on
I would ues the reverse iterator.
1
2
3
4
5
6
7
  
  string s1("anna");
  string s2(s1.rbegin(), s1.rend());
  if (s1 == s2)
  {
    // palindrome
  }
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>
using namespace 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);
}
Last edited on
knocking a couple lines off the above.

1
2
3
4
5
6
string Word1, outs[2] {"match","no match"};	
	cout << "Please enter a word" << endl;
	cin >>Word1;
	string  Word2(Word1.rbegin(), Word1.rend());
    cout << outs[Word1!=Word2] << endl;
	return(0);


I'd like to get rid of word 2 but I don't see a way to do it.
Last edited on
I don't do this often but this is a bit more condense, imo.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string Word, outs[2] {"","no "};	
    cout << "Please enter a word\n";
    cin >> Word;
    cout << outs[Word != string(Word.rbegin(), Word.rend())] << "match" << endl;
}
Last edited on
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.
Last edited on
1
2
3
4
5
6
7
#include <iostream>
#include <string>
int main(){
    std::cout << "Please enter a word\n";
    auto s = [](){ std::string s; std::cin >> s; return s; }();
    std::cout << "no " + (s == std::string(s.rbegin(), s.rend())) * 3 << "match" << std::endl;
}
Last edited on
ASCII code 3, end of text. Black magic?
Edit: Wait I have no idea what's happening.

cout << "no " + 3 << "match" << endl; --> "match"
cout << "no" + 3 << "match" << endl; --> "matchmatch" ???
Last edited on
That's subtle.

"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.

Nice obfuscation.

Could be
 
    std::cout << "no match\n" + (s == std::string(s.rbegin(), s.rend())) * 3;

I'm not sure what the lambda is for, though. It just adds characters (if the goal is to be as minimal as possible).
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.
Last edited on
tpb: Nice. I didn't think of that one.

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...).
Last edited on
> I'd like to get rid of word 2 but I don't see a way to do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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/equal
        const bool palidrome = std::equal( word.begin(), word.begin()+word.size()/2, word.rbegin() ) ;

        std::cout << "the word " << ( palidrome ? "is" : "is not" ) << " a palindrome\n" ;
    }
}
Topic archived. No new replies allowed.