What is your current code? It looks like you are defining reverse twice, or there is a conflict with the standard library version.
What is your current code? It looks like you are defining reverse twice, or there is a conflict with the standard library version.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
everything in my last post is my current code...i didn't change anything from that..
Oh, I see the problem now:
You should not be including "pal.cc". You should compile pal.cc separately and link it with the object file produced by compiling main.cc (or whatever source file the main function is in).Originally Posted by jewelz
Instead of including <iostream> in pal.h, include <string> since you refer to std::string in pal.h.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
ok well this is what i came up with for printing the the palindromes:
now for the last part, the program will take input and output the longest palindrome, along with its length..i came up with this:Code:int main() { string s; while(cin >> s){ if(isPalindrome(s)== true) cout << s << endl; } return 0; }
does this seem ok?Code:int main() { string s; vector<string> words; while(cin >> s) && (isPalindrome(s)== true) { words.push_back(s); } string longest = words[0]; for (int i = 1; i != words.size(); i++) { if(words[i].length() > longest.length()) { longest = words[i]; } cout <<"Longest Palindrome is " << "\"" << longest << "\"" << "whose length is " << longest.length() << endl; return 0; }
Last edited by jewelz; 02-22-2009 at 02:35 PM.
you do not need to store all the strings to find the longests
enogh to have 2 strings - longest_so_far and current
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
um..can u explain that a little more please? my method wouldn't output the longest palindrome?
Last edited by jewelz; 02-22-2009 at 03:53 PM.
It would, eventually. But you don't need to store all the words, just the longest (unless you need to keep all the palindromes for other reasons).
ok is this better??
Code:int main() { typedef string:: size_type string _size; string s; string_size max = s.size(); string longest = s; while(cin >> s) && (isPalindrome(s)== true) { string_size size = s.size(); if (size > max) { max = size; longest = s; } } cout <<"Longest Palindrome is " << "\"" << longest << "\"" << "whose length is " << max << endl; return 0; }
you've got erroneous spaces in your typedef.
is everything good now?? =)Code:typedef string::size_type string_size;
Last edited by jewelz; 02-22-2009 at 04:37 PM.
that looks good
incidentally,i think it's very odd you weren't encouraged to do something like this:
Code:bool isPalindrome(const std::string& s) { std::string::const_iterator fit = s.begin(); std::string::const_iterator rit = s.end(); --rit; for(unsigned int i=0;i<s.size()/2;i++,fit++,rit--) { if(*fit==' ') ++fit; if(*rit==' ') --rit; if(*fit != *rit) return false; } return true; }
hmm..getting a compilation error..it's saying:
error: expected identifier before '(' token
indicating the line with while (cin >> s)