I really need help, this is important. i have no idea how to write this code?

I need to write the function "isPalindrome()" so that it loops through the given word and tests whether the word reads the same forward and backward. A palindrome is a word that is the same forwards and backwards. The word I want to use is SAGAS. The language I am using to write the code is JavaScript and I would like it to be an if statement withing the while loop that I have below. Here is what I have so far:

function isPalindrome(SAGAS)
{
var left = 0, right = phrase.length - 1;
var palindrome = true;

while (palindrome && left < right)
{



closed account (SECMoG1T)
This is c++ but you might convert it to suit you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 bool palidrome ( const string& s, int start, int stop) /// ths is a recursive function   
  {
       if (s.size ()==1 || start>=stop)
              return true;
       else if (s [start]!=s [stop])
              return false;
       else
         {
             start +=1;  stop -=1;
              return palidrome (s,start, stop);
          }
     }              

 bool ispalidrome (const string & s) /// wrapper function
  {
     int start=0, stop=s.size ()-1;
     return palidrome (s, start, stop);
   } 

  /// in main

   string s ("eve");
   if (ispalidrome (s))
     /// do something
 
Last edited on
i came up with a non recursive solve.
but first some help
how would one go about comparing the word sagas?
compare the first to the last letter
then compare the second and the fourth letter,and so on
the middle letter is the same as itself ofcourse no need to test that.


here is my solve,but please try and come up with your own solution,it might be even better!
1
2
3
4
5
6
7
8
for(int i =0;i<str.length();i++){
    if(str[i]==str[stringLength-i-1]){
        isPalindrome=true;
            }else{
                isPalindrome=false;
                break;
                }
    }

Last edited on
Topic archived. No new replies allowed.