So I'm trying to solve this problem that asks to look for palindromes in strings, so seems like I've got everything right, however the problem is with the output.
Here's the original and my out put:
http://pastebin.com/c6Gh8kB9
Here's whats been said about input and input of the problem:
Input format :
A file with no more than 20,000
characters. The file has one or more
lines. No line is longer than 80
characters (not counting the newline
at the end).
Output format :
The first line of the output should be the length of the longest
palindrome found. The next line or
lines should be the actual text of the
palindrome (without any surrounding
white space or punctuation but with
all other characters) printed on a
line (or more than one line if
newlines are included in the
palindromic text). If there are
multiple palindromes of longest
length, output the one that appears
first.
Here's how I read the input :
string test;
string original;
while (getline(fin,test))
original += test;
And here's how I output it:
int len = answer.length();
answer = cleanUp(answer);
while (len > 0){
string s3 = answer.substr(0,80);
answer.erase(0,80);
fout << s3 << endl;
len -= 80;
}
cleanUp() is a function to remove the illegal characters from the beginning and the end. I'm guessing that the problem is with \n's and the way I read the input. How can I fix this ?
No line is longer than 80 characters (not counting the newline at the end)
does not imply that every line is 80 characters except for the last, while your output code does assume this by taking 80 characters off answer in every iteration.
You may want to keep the newlines in the string until the output phase. Alternatively, you might store newline positions in a separate std::vector. The first option complicates your palindrome search routine; the second your output code.
(If I were you, I'd also index into answer instead of taking chunks off with substr/erase; your output code is now O(n^2) while it could be O(n).)
After rereading, it appears that I misunderstood the question. I was thinking in terms of each line representing a single word, and the intent is to test whether that "word" is palindromic.
After rereading, I think the question is really more like: "Given a sequence of up to 20,000 characters, find the longest palindromic sub-sequence. Oh, incidentally, the input is broken up into lines of no more than 80 characters."
If that's correct, I'd ignore the line-length completely. I'd read the entire file into a single buffer, then search for palindromes in that buffer.
To find the palindromes, I'd simply walk through each position in the array, and find the longest possible palindrome with that as its center point:
for (int i=1; i<total_chars; i++)
for (n=1; n<min(i, total_chars-i); n++)
if (array[i+n] != array[i-n])
// Candidate palindrome is from array[i-n+1] to array[i+n-1]
Related
I'm at my wits' end here (which isn't saying very much). My program takes two forms of user input, a sentence and a word to be stripped from that sentence. The sentence has a fixed size of 101 characters, and the word to be stripped, has a fixed size of 5. Although strings and vectors would make this much easier, I would like to only use char arrays.
The problem that I seem to be having is comparing arrays of two different sizes. I try to loop through the sentence length and compare each individual element in the sentence and key word to be removed. The issue is that I can't get the key word to compare with every word in the sentence, but only the first (if there's a match). The code to compare these two char arrays is
for(int i = 0; i < strlen(sentence); i++)
{
if (sentence[i] == keyword[i])
sentence[i] = ' ';
}
The thought process here is to directly compare each individual element, and if there's a match, replace that instance with a space, to 'deconstruct' the sentence. That way, the user can see how their sentence looks after removing all instances of their 'key' word entered. For example, the keyword 'anti' compared to the sentence 'antivenom is antinice' would yield 'venom is nice.'
Assume that all user input is converted to lowercase and punctuation is removed to ease this comparison.
What am I doing wrong here? How can I compare the 'key' word to every single word found in the user sentence? Any advice would be greatly appreciated. Thank you.
I have multiple log files of NMEA sentences that contain geographical positions captured by a camera.
Example of one of the sentences: $GPRMC,100101.000,A,3723.1741,N,00559.5624,W,0.000,0.00,150914,,A*63
My question is, how do you reckon I can start on that? Just need someone to push me to the right direction, thanks.
I use this checksum function in my GPSReverse driver.
string chk(const char* data)
{
// Assuming data contains a NMEA sentence (check it)
// Variables for keeping track of data index and checksum
const char *datapointer = &data[1];
char checksum = 0;
// Loop through entire string, XORing each character to the next
while (*datapointer != '\0')
{
checksum ^= *datapointer;
datapointer++;
}
// Print out the checksum in ASCII hex nybbles
char x[100] = {0};
sprintf_s(x,100,"%02X",checksum);
return x;
}
And after that, some append to the NMEA string (say, GGA) :
string re = chk(gga.c_str());
gga += "*";
gga += re;
gga += "\r\n";
So you can read up to the *, calculate the checksum, and see if it matches the string after the *.
Read more here.
Each sentence begins with a '$' and ends with a carriage return/line
feed sequence and can be no longer than 80 characters of visible text
(plus the line terminators). The data is contained within this single
line with data items separated by commas. The data itself is just
ascii text and may extend over multiple sentences in certain
specialized instances but is normally fully contained in one variable
length sentence. The data may vary in the amount of precision
contained in the message. For example time might be indicated to
decimal parts of a second or location may be show with 3 or even 4
digits after the decimal point. Programs that read the data should
only use the commas to determine the field boundaries and not depend
on column positions. There is a provision for a checksum at the end of
each sentence which may or may not be checked by the unit that reads
the data. The checksum field consists of a '' and two hex digits
representing an 8 bit exclusive OR of all characters between, but not
including, the '$' and ''. A checksum is required on some sentences.
So i have this text file which basically has 2 columns of letters and numbers separated spaces. I want to split these 2 columns and place them in separate arrays.
I tried using the getLine method with space as the delimiter but I am only able to place them in the same array. I can do this with fileOpen.eof method but that causes too many problems in my program
while(getline(openFile, letters, ' ')){
index++;
lettersArray[index] = letters;
}
I expect the output of lettersArray[index] to be a column of letters only.
I think you are using the getline function in the wrong way. Take a look at how it works here: http://www.cplusplus.com/reference/string/string/getline/
You are basically telling the getline function to use the space character to use as the delimiter. So it is processing the letters in the file in the odd numbered iterations of the while loop and the numbers in the file in the even numbered iterations of the while loop.
If you want to stick to using the getline function, here is a possible modification to make it work.
while(getline(openFile, letters, ' ')){
index++;
lettersArray[index] = letters;
getline(openFile, letters);
}
The call to the getline function on the last line of the while loop, gets rid of the remaining part of the current line.
So I'm not entirely sure why this is happening. I've tried just adding in spaces before the words in the txt file that I'm reading from and it fixes it for some, but not all. Basically I'm just trying to return a name, and each name in the file is on a different line. But when i print the names, some of them are cut off, like "Dillon" would be "llon" or "Stephanie" will be "phanie" and so on. Here's the use of seekg:
string Employee::randomFirstName()
{
int i;
string fName;
i = rand() % 100;
ifstream firstName;
firstName.open("First Names.txt", ios::out);
firstName.seekg(i);
firstName >> fName;
return fName;
}
So, I would post the txt file, but its just a list of names, one per line, 100 of them. I've tried looking up examples of the use of seekg, but I cant seem to figure out why it cuts off some. Also, it only cuts off sometimes. One output it'll print out "Dillon" right, next it would print "llon".
Any help would be appreciated
istream::seekg() will move to a character position. Therefore, seeking to a random character position between 0 and 99 (rand() % 100) may end up in the middle of a line. There is no way for seekg to know you wanted to seek to a line number: it has no concept of lines.
You can instead use std::getline for i number of times to get to that specific line.
Let's say that we have a string declared...
string paragraphy = "This is a really really long string containing a paragraph long content. I want to wrap this text by using a for loop to do so.";
With this string variable I want to wrap the text if it is more than 60 width and if there is a space after those the 60 width.
Can someone please provide me with the code or any help in creating something like this.
A basic idea to solving this is to keep track of the last space in a segment of the string before the 60th character in that segment.
Since this is homework, I'll let you come up with the code, however here's some rough pseudo-code of the above suggestion:
- current_position = start of the string
- WHILE current_position NOT past the end of the string
- LOOP 1...60 from the current_position (also don't go past the end of the string)
- IF the character at current_position is a space, set the space_position to this position
- Replace the character (the space) at the space_position with a newline
- Set the current_position to the next character after the space_position
- If you're printing the string rather than inserting newline characters into it, you would print any remaining part of the string here.
You might also want to consider the case where you don't have any spaces in a block of 60 characters.