#Take care with inputs and outputs
Spot the problem here:
char testString[BUFSIZ];
scanf("%s", testString);
Although testString only has enough space to hold BUFSIZ characters (including the string terminator), we read an unbounded string from standard input. And never test the result of scanf, so we don't know whether any characters were even read.
There's also no checking of the result of printf() - that's arguably not a problem here, but in an interview question, I'd expect to see a comment explaining that you have consciously chosen to skip the check that you would normally put in your production code.
The buffer overrun isn't a problem in the C++ version (as a std::string will expand as necessary), but there is a missing check of cin.good() after reading from it. The idiomatic form is something like
if (std::cin >> PossiblePalindrome) {
// ...
} else {
throw std::runtime_error("Failed to read input");
}
#Write for localisation Assembling strings like this can harm the localisability of your messages:
printf("The test string %s is %s a palindrome\n", testString, (iSPalendrome? "" : "Not"));
Not all languages can negate a sentence with a single change to one part like this, so if your program were to be internationalized and ship with message catalogs for each language, this statement would probably need to be re-written. Perhaps something like the following would be better:
printf(iSPalindrome
? "The test string %s is a palindrome\n"
: "The test string %s is not a palindrome\n",
testString);
But I'd lean towards an if/else so that the compiler can still check that the arguments agree with the format string.
The C++ exhibits a similar problem. Additionally, the C version will output a doubled space ("is a") for the negative case. That could have been avoided by moving one of the spaces into the "Not" - but don't compose strings like that anyway.