ah darn it...using the STL reverse algorithm would be the easiest way, but i'm not allowed to use it..owned
ah darn it...using the STL reverse algorithm would be the easiest way, but i'm not allowed to use it..owned
the reversing algorithm is trivial. you have that part more-or-less correct, though it can be done more easily
std::string::iterator is a part of std::string; you appear to be allowed to use std::string.
using that site, i came up with this...i really gotta figure this out...sigh..
Code:string s; string::reverse_iterator rit; for ( rit=s.rbegin() ; rit < s.rend(); rit++ ) return rit;
jewelz, you're still plagued by a basic misconception about how functions work.
a function only returns at most ONE value each time it is called. a function with a return type specification of void never returns a value.
a return statement immediately stops execution of the function and returns the variable specified by the argument of the return statement.
so, in your pseudofunction above, the for loop accomplishes nothing, because the function will return rit on the first iteration, which is exactly equivalent to:
Code:return s.rbegin();
the basic anatomy of a function is as follows:
you would use a function like this:Code:// a free function (i.e. a function that is not a member function of a class) returnType functionName(/*argument list here*/) { // function body return instance_of_returnType; } // a class-member function returnType className::functionName(/*argument list here*/) { // function body return instance_of_returnType; }
note how the vector is populated over the course of the function execution and then ONE result is returned.Code:#include <vector> std::vector<int> countTo(unsigned int number) { std::vector<int>countList; for(unsigned int i=0;i<number;i++) { countList.push_back(i+1); } return countList; } int main(int argc, char* argv[]) { std::vector<int> oneToTen = countTo(10); }
Last edited by m37h0d; 02-16-2009 at 07:08 PM.
this is so frustrating...so basically i have to return the reversed string right? how would i do that..i cant just "return s;" that makes no sense i think..or should i create another string and then += that to the old string? im sorry im such a noob..but i really appreciate the help..
SOOoooo close.
you wouldn't += onto the "old string", but you would += elements of the "old string" onto the new string. i think that's what you meant, but in programming these semantics are very important
you could modify the string passed into the function argument, except that int your suggested prototype it is passed by const reference. that means you are not allowed to modify it.
don't sweat it. you read a documentation page. that's a victory in and of itself imo. RTFM is job 1
ok so creating the new string i would just declare:
string s2;
and then im not sure, how do i include this in the for loop?
final try for tonight...what do you guys think?
Code:string s2 = ""; for (int i = 1; i < s.length; i++) { s2 = s2 + s[s.length - i]; } s2 = s2 + s[0]; return s2;
If you are allowed to use std::string::reverse_iterator, then from original_string.rbegin() to before original_string.rend(), copy to the new string.
If you must use indices, then copy the original string into a new string, then reverse the old string by swapping the nth character with the nth last character, all the way from the character at position 0 to the middle of the string.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
ok guys, so now i have to write a main function that will read words from input and print out all the palindromes, each being on a seperate line..
so first of all my professor said to use the bool palindrome function in terms of the reverse, so i came up with this:
and as for the main function should i do something like:Code:bool Is_Palindrome(const std::string& s) { if (s==reverse(s)) return(true); else return(false); }
i tried compiling it, but it gave me some really weird error..and its not even pointing at specific lines..Code:int main() { string s; cout << "Input some words "; getline (cin, s); if (if (Is_Palindrome(s)==true) cout<< s <<endl; return 0; }
anyway let me give u guys the format i have for this program maybe it will help:
i have a header file called pal.h :
a file called pal.cc which includes the definitions of the above functions:Code:#ifndef GUARD_pal_h #define GUARD_pal_h #include <iostream> std::string reverse(const std::string& s); bool is_palindrome(const std::string& s); #endif
and as for the main function i posted above i included "pal.h" and "pal.cc" and then the appropriate declarations and usings..what might be the problem?Code:#include "pal.h" #include <string> using std::string; //function declaration std::string reverse(const std::string& s) { string s2 = ""; for (int i = 1; i < s.length(); i++) { s2 = s2 + s[s.length() - i]; } s2 = s2 + s[0]; return s2; } bool Is_Palindrome(const std::string& s) { if (s==reverse(s)) return(true); else return(false); }
Last edited by jewelz; 02-21-2009 at 12:22 PM.
this is simplerCode:bool Is_Palindrome(const std::string& s) { return(s==reverse(s)); }
Well, your earlier method, the IsPalindrome function on the first page, is a lot better... Why even both reversing it all? I don't understand why your professor should say something like that... As a bad hint or as yet another silly must?
it's still not compiling properly..how should i fix up my main function?
this is the type of error im getting:
pal.o: In function `reverse(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
pal.cc: (.text+0x5c): multiple definition of `reverse(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
proj3a.o: proj3a.cc: (.text+0x5c): first defined here
pal.o: In function `Is_Palindrome(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
pal.cc: (.text+0x217): multiple definition of `Is_Palindrome(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
proj3a.o: proj3a.cc: (.text+0x217): first defined here