Palindrome Coding trouble

This is a discussion on Palindrome Coding trouble within the C++ Programming forums, part of the General Programming Boards category; I am trying to code a Palindrome program...I keep on getting a runtime error called Index out of range: 3 ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    15

    Palindrome Coding trouble

    I am trying to code a Palindrome program...I keep on getting a runtime error called

    Index out of range: 3 string: mom

    yeah I can't figure out why though...here's my code
    Code:
    void Palindrome()
    {
    	apstring Input;
    	apvector<char> Word;
    	apvector<char> ReversedWord;
    	int Length = 0;
    	int c = 0;
    	int d = 0;
    	bool Palindrome = true;
    
    	cout << endl;
    	cout << "Enter in a word to see if it is a palindrome: ";
    	cin >> Input;
    
    	ToLower(Input);
    	Length = Input.length();
    	
    	Word.resize(Length);
    	ReversedWord.resize(Length);
    
    	for(int b = Length; b > 0 ; b--)
    	{
    		Word[d] = Input[b];
    		d++;
    	}
    
    	for(int i = Length; i > 0; i--)
    	{
    		ReversedWord[c] = Word[i];
    		c++;
    	}
    
    	for (int e = 0; e < Length; e++)
    	{
    		if(ReversedWord[e] == Word[e])
    		{Palindrome = true;}
    		else
    		{Palindrome = false;}
    	}
    	
    	if (Palindrome == true)
    	{cout << "This word is a palindrome." << endl;}
    	else
    	{cout << "This word is not a palindrome." << endl;}
    
    }
    yeah this is it by the way my headers are

    <iostream.h>
    <strutils.h>
    <apstring.h>
    "apvector.h"


    yeah thanks for your help

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    try this:

    Code:
    apstring Input("temporary");
    it has to do with that space is not alloted for the string. i am not quite sure why, it has done that to me as well.

    if you pass it as a parameter, you could avoid asking for them to input the word into the function.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    15
    meh same error still...I would just use vectors but the problem is knowing what size to put it at for efficiency(sp?)...so I use a apstring to get the word and its length...and use the length for the size of the vector...but the part that I think screws it up is transferring the apstring into the vector....so yeah I can't figure it out still...offer idea's that are most help ful....

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    15
    meh nvm I finished coding it myself....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Error in Recursive String Palindrome Code
    By clegs in forum C Programming
    Replies: 13
    Last Post: 12-21-2008, 11:36 AM
  3. Is it a Palindrome?
    By xp5 in forum C Programming
    Replies: 3
    Last Post: 09-06-2007, 05:26 AM
  4. palindrome trouble
    By edshaft in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 09:24 AM
  5. palindrome HELP !!!
    By TED22_8 in forum C Programming
    Replies: 23
    Last Post: 01-22-2002, 01:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21