Monday, July 27, 2009

C++ help me pls!?

could you write this program for me?


in c++


palindrome strings like {did,dad,rader}


a program with resoursive function that check the string which stored in array if palindrome string or not !if it is palindrome return true ,


else ,return false.


thank you

C++ help me pls!?
I won't write the code for you, since it sounds like a homework assignment, but I can give you the pseudocode you can use:





string is "s"


"a" is an int


"p" is a boolean value which will be the return value








p = TRUE


for a = 0 to length(s)/2


__if a'th digit from the left != a'th digit from the right


____p = FALSE


____end loop





return p
Reply:First you should do your own homework but with the following, at least find out what it is doing and understand the logic.





string Test;


cin %26gt;%26gt; Test;


bool bFlag = true;





string::iterator IterFw = Test.begin();


string::reverse_iterator IterBw = Test.rbegin();


while( *(IterFw++) == *(IterBw++) %26amp;%26amp; IterFw != Test.end() ) {


bFlag = false;


}





if( !bFlag )


cout %26lt;%26lt; "it's a palindrome" %26lt;%26lt; endl;


else


cout %26lt;%26lt; "it's NOT a palindrome" %26lt;%26lt; endl;





Some more hints to solve your problem - before you feed the string to the palindrom algorithm you have to do some preprocessing like stripping the blanks. A possible but not very efficient way would be to go through the string character by character and copy those that are not blanks into a new


string. Another way would be to consider the remove_if() function with a predicate that should look like std::bind2nd(std::equal_to%26lt;char%26gt;(), ' ')





In order to make lower case comparisons you'll have to modify the condition of the while loop given above, a little bit. At the moment it compares character by character taking upper and lower case into account. I guess this should get you started.
Reply:Checking for palindromes





* Assume that the maximum length of a palindrome is 30.


* A palindrome can be defined recursively as follows:


1. A empty string is a palindrome. (base case)


2. A single letter is a palindrome. (base case)


3. If the first and last letters are the same, and the internal letters are a palindrome, then it's a palindrome. (recursive case)


* Formulatting the above in C++ yields the implementation (Note - depending on your compiler, your mileage might vary):





#include %26lt;iostream.h%26gt;


#include "boolean.h"





const int MAX_STRING_SIZE = 30;


typedef char string[MAX_STRING_SIZE+1];





//--------------------------------------...


boolean Palindrome(string TheString, int Start, int Length);





//--------------------------------------...


int main(void)


{


string InputString;





cout %26lt;%26lt; "Enter a string: ";


cin %26gt;%26gt; InputString;


cout %26lt;%26lt; InputString %26lt;%26lt; " is ";


if (!Palindrome(InputString,0,strlen(InputS...


cout %26lt;%26lt; "not ";


cout %26lt;%26lt; "a palindrome" %26lt;%26lt; endl;


return 0;


}





//--------------------------------------...


boolean Palindrome(string TheString, int Start, int Length)


{


if ((Length == 0) || (Length == 1)) // base cases


return TRUE;


else // recursive cases


{


if (TheString[Start] == TheString[Start + Length - 1])


return(Palindrome(TheString, Start + 1, Length - 2));


else return(FALSE);


}


}
Reply:Thumbs down for the dorks that did her homework for her. Maybe you hope she'll be your girlfriend now?

snow of june

No comments:

Post a Comment