# help :D

Discussion in 'C' started by cooop, Jul 27, 2009.

1. ### cooopNew Member

Joined:
Jul 8, 2009
Messages:
19
0
Trophy Points:
0
2. Write a C++ program that reads data from an input file called “Input.txt” and counts how many times the file contained words that are the same as their inverse. Such words are called e. The input file can be downloaded from the BB. The program should print the number of palindromes found in the text on the screen, and also saves a copy of each palindrome found in the input file in another file (output file) called “palindromes.txt”.

is this true ? ;D
Code:
```#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
ifstream fin;
ofstream fout;
bool PalindromeChecker(string inputString)
{
int i=0;

int length = inputString.length();
char temp = {'\0'};

for (i=0; i<length; i++)
{

if ((temp[i] >= 'a') && (temp[i] <= 'z'))
temp[i] = char(int(temp[i]) - 32);
}
for (i=0; i<length; i++)
{

if ((temp[i] < 'A') || (temp[i] > 'Z'))
{
for (int j=i; j<length; j++)
{
temp[j] = temp[j+1];
}
i--;
length--;
}
}

int checkLength = length - 1;

if ((checkLength == 0) || (checkLength == 1))
return true;
for (i=0; i<=checkLength/2; i++)
{
if (temp[i] != temp[checkLength-i])
return false;
}
return true;
}
{
if (isit == true)
cout << input.data() << " is a palindrome." << endl;
else
cout << input.data() << " is not a palindrome." << endl;
}
int main()
{
string inputString;
bool isPalindrome = true;
fin.open("palindromes.txt");
fout.open("output.txt");
if (!fin.good())
throw "I/O Error!";
if (!fout.good())
throw "I/O Error!";
while (fin.good())
{

getline(fin,inputString);

isPalindrome = PalindromeChecker(inputString);

}
fin.close();
fout.close();
return 0;
}```

Last edited by a moderator: Jul 28, 2009
2. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
Please use code blocks when you post code. You've been around long enough now to know about them. It makes code easier to read by preserving the formatting and using a fixed pitch font.

Rather than me reading the code and trying to figure out if it's correct or not, why don't you plug it into your favourite compiler, write some test data and see what the program does with that data. If it picks out the right data and ignores the wrong, then it works. If it doesn't, then it doesn't.

If you display the contents of temp just before returning from PalindromeChecker(), passing in some known words, does temp contain what you expect?

3. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
tried the code in Visual Studio 2008. Reduced the code to:
Code:
```char *str="HellolleH";
printf("%s %s a palindrome\n",str,PalindromeChecker(str)?"IS":"IS NOT");
```
and it (wrongly) said IS NOT. Have a look at the code in the PalindromeChecker function and tell me what you think the first for() loop will do. Will that "if" ever evaluate true?