NOT WORKING AS EXPECTED, HELP PLZ

Apr 1, 2014 at 6:48pm
closed account (ivDwAqkS)
what is the error here? it is always outputting that it is not a palindrome. why?
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
26
27
28
29
30
31
32
33
34
void palindrome (string s){
    int get_middle, middle;
    int word_size = s.length();
    int i;
    int counter = 0;

    if(word_size%2 == 1){
        middle = (word_size/2);
        get_middle=(word_size/2)+1;
        for(i = 1; i <= middle; i++){
            if(s[(get_middle-i)] == s[(get_middle+i)])
                    ++counter;
            else
                break;
        }
        if(counter == middle)
            cout << s << " is a Palindrome";
        else
            cout << s << " is NOT a Palindrome.";
    }
    else if(word_size%2 == 0){
        get_middle = (word_size/2);
        for(i = 0; i < get_middle; i++){
            if(s[i] == s[word_size-i])
                counter++;
            else
                break;
        }
        if(counter == (get_middle-1))
             cout << s << " is a Palindrome";
        else
            cout << s << " is NOT a Palindrome.";
    }
}
Last edited on Apr 1, 2014 at 6:48pm
Apr 1, 2014 at 8:05pm
closed account (ivDwAqkS)
I found the problem that is the if stamen of the for loops

1
2
3
4
5
if(s[(get_middle-i)] == s[(get_middle+i)])// not taking this statement, why?
                    ++counter;
            else
                break;


1
2
3
4
 if(s[i] == s[word_size-i])//not taking this statement, why?
                counter++;
            else
                break;
Apr 1, 2014 at 8:28pm
They way you are doing it, if the word has an even number of letters, say 6, the middle would equal 3.5 and that is being truncated to 3, there for messing up the entire program from there on. Also for an odd numbered word, say 5, the middle would be 3 but you are saying get_middle is 4. So you would then check the 3rd letter against the 5th letter.

The best way for you to do this is to have to nested for loops, one counting up and one counting down. If the program detects that the elements in the area are equal, it adds to counter and breaks out of the inner loop, otherwise it just breaks out of the loop.
Last edited on Apr 1, 2014 at 8:58pm
Apr 1, 2014 at 9:17pm
closed account (ivDwAqkS)
but it is an int value so, it doesnt take decimals, am i wrong?
 
int get_middle, middle;
Last edited on Apr 1, 2014 at 9:18pm
Apr 1, 2014 at 9:19pm
You aren't wrong, I was just pointing out some reasons why it isn't working, I would suggest trying the nested for loop way.
Apr 1, 2014 at 9:23pm
closed account (ivDwAqkS)
thanks, anyway and the reason i want to know why it doesnt work , its because i need to know how to resolve this, but i can do the nested loop ,too. but if someone has the answer please post it. i will try to find it out.
Apr 1, 2014 at 9:25pm
I don't think that there is a good answer to the way you have been trying to do that is near as easy or effictient as using the loops. I don't even know if there is an answer.
Apr 1, 2014 at 9:37pm
Your palindrome seems a bit overly complicated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool palindrome(const std::string &str)
{
    for(int lhs = 0, rhs = str.size()-1; lhs < rhs; ++lhs, --rhs)
    {
        if(str[lhs] != str[rhs]) return false;
    }
    return true;
}

//or

bool palindrome(const std::string &str)
{
    for(auto lhs = str.begin(), rhs = str.end()-1; lhs < rhs; ++lhs, --rhs)
    {
        if(*lhs != *rhs) return false;
    }
    return true;
}


A third possible method:

1
2
3
4
5
bool palindrome(const std::string &str)
{
    const std::string reverse(str.rbegin(), str.rend());
    return str == reverse;
}
Last edited on Apr 1, 2014 at 9:39pm
Apr 2, 2014 at 12:34am
HI xdmelasxx (74),
In your code there were 3 conceptual error:
1) As u had identified the compiler does not enter the if loop as expected.Because the if statement is with respect to array. if you input as xahax , the first x is x[0] but according to you prog it takes it as x[1].Hence reduce I to 0.(note s is an array so s[0] is your 1st element)
Correct code:
for(i = 0; i < middle; i++){
if(s[(get_middle-(i + 2))] == s[(get_middle+i)])
++counter;
2) the same array logic changes your 2nd for statement to :
for(i = 0; i < get_middle; i++){
if(s[i] == s[word_size-(i+1)])
counter++;
3) There is a simple logic issue in your if statement :
if(counter == (get_middle))
{cout << s << " is a Palindrome";
Last edited on Apr 2, 2014 at 2:44pm
Apr 2, 2014 at 12:46am
[center][tt]
#include <iostream>
#include "conio.h"
#include <string>
#include <ostream>
using namespace std;


void palindrome (string s){
    int get_middle, middle;
    int word_size = s.length();
    int i=0;
    int counter = 0;

    if(word_size%2 == 1){
        middle = (word_size/2);
        get_middle=(word_size/2)+1;
        for(i = 0; i < middle; i++){
            if(s[(get_middle-(i + 2))] == s[(get_middle+i)])
                   ++counter;
            else
			 break;

        }
       if(counter == middle)
            { cout  <<s<<" is a Palindrome";
			_getch();
		}
        else {
            cout << s << " is NOT a Palindrome.";
			_getch();}
    }
    else if(word_size%2 == 0){
        get_middle = (word_size/2);
        for(i = 0; i < get_middle; i++){
            if(s[i] == s[word_size-(i+1)])
                counter++;
            else
                break;
        }
        if(counter == (get_middle))
             {cout << s << " is a Palindrome";
		_getch();}
        else
            {cout << s << " is NOT a Palindrome.";
		_getch();}
    }
}

int main(){
string  s;
cout <<" enter a string";
cin >> s;
palindrome(s);
return 0;
}
/output][/center]

I have used visual studio 2010 compiler. It works
Last edited on Apr 2, 2014 at 2:50pm
Apr 2, 2014 at 12:48am
Conio is non-standard and so is void main it should return an integer.
Apr 2, 2014 at 2:40pm
thankx giblit corrected the minor errors by editing the prog
Apr 2, 2014 at 2:58pm
closed account (ivDwAqkS)
keval thanks. however what is this for?
 
_getch();

can you add comments to your lines please? D:
Last edited on Apr 2, 2014 at 3:16pm
Apr 2, 2014 at 3:10pm
_getch(); is in input operator that takes each character as it is inputted, without the user having to press enter. It is like a cin>> that only takes one character of input and doesn't require the user to press enter.
Apr 2, 2014 at 3:17pm
closed account (ivDwAqkS)
and about my logic of the first if statement, if you writte:
tenet
it has 5 characters right?
so get_middle = (2)+1; then it takes 3 as the middle, so when i say
1
2
3
4
5
for(i = 1; i <= middle; i++){
            if(s[(get_middle-i)] == s[(get_middle+i)]) 
           //if(s[3 - 1] == s [ 3 + 1] then counter = 1 (it analyze t(e) n (e)t) am i right?
                   ++counter;
        //so its suppose to do the same until -i- gets the number 2 in this case 

but the problem is , that the if statement is not taken, and i would like to know why and i will appreciate again other answers and someone could again explain keval post? coz i don't know the conio.h library, by the way i use code::blocks for now.
Last edited on Apr 2, 2014 at 3:27pm
Apr 2, 2014 at 3:29pm
//if(s[3 - 1] == s [ 3 + 1] then counter = 1 (it analyze t(e) n (e)t) am i right?
NO thats not the case it compares s[2] with s[4] that is te(n)e(t)

Consider the following
s[0]=t
s[1]=e
s[2]=l
s[3]=e
s[4]=t so u need to compares[0] with s[4]
and s[1]=e with s[3]
if loop in your case:
if(s[(get_middle-i)] == s[(get_middle+i)])
reduce it down....
it compares s[2] with s[4] (when i =1) which is incorrect.

Hence
if(s[(get_middle-(i + 2))] == s[(get_middle+i)])
++counter;

dont worry for syntax; the flaw is in the logic
Last edited on Apr 2, 2014 at 3:33pm
Apr 2, 2014 at 4:32pm
closed account (ivDwAqkS)
thank you so much bro :o, thanks keval :D
Topic archived. No new replies allowed.