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.
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.
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.
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";
_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.
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.
//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.