In my program I am supposed to read a text file in line by line and search for the longest palindrome and return its line number.
Each text file is 100,000 lines long with a max length of 15.
With my code, I am able to read each line into
char lines[100000][15]
Except for the blank lines which throw off my calculation of which line contains the longest palindrome.
For example a file containing: (0: is line 0, 1: is line 1, ect.)
0: hello
1: bob
2: joe
3:
4: cat
Comes up as:
0: hello
1: bob
2: joe
3: cat
4: (whatever 5: would be)
Here's my code for reading the file:
std::ifstream theFile;
theFile.open(argv[1]);
char lines[100000][15];
for (int i = 0; i < 100000; i++)
{
for (int j = 0; j < 15; j++)
{
lines[i][j] = '\0'; //I do this to initialize each char to null
}
}
while (!theFile.eof())
{
for (int i = 0; i < 100000; i++)
{
theFile >> lines[i];
}
}
I'm assuming the issue is with the line:
theFile >> lines[i];
not copying over newlines or other formatting characters, but I'm not sure how to get around this so any help would be appreciated.
I have to use an array of char arrays, btw, because I am using MPI to pass the data and I can only send chars and not arrays/strings.