Can't Define a String as another String backwards

//I can't see any issues with the way I was using to define String back as //simstring backwards, but I get an on Output Error, saying:
//index out of range: 0 string:
//Note: This program is not completed (obviously). I still have to account for //multiple word palindromes, etc., but I was trying to get it done properly with //single words before I moved on.


/* Project Palindrome
by Jacob Wright 2-21-13 */

#include <iostream.h>
#include <lvp\string.h>
#include <stdlib.h>

//________________________________

void assimilate(String simstring); //Removes capitalization differences and punctiation from simstring

//________________________________

void compare(String oristring, String newstring){} //Tests if both strings are the same, and outputs

//________________________________

int main()
{
String input;
cout << "\n\n\n\n\n\t\t Input a String to be tested: ";
cin >> input;
cout << flush;
system("cls");
cout << "\n\n\n\n\n";
assimilate(input);
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t ";
return(0);
}

//_________________________________

void assimilate(String simstring)
{
for (int c=0; c<simstring.length(); c++)
{
if ((simstring[c]>='a') && (simstring[c]<='z'))
simstring[c]=simstring[c]-'a'+'A';
}

cout << "\t\t Original String: " << simstring;
cout << "\n\n\n\t\t Backwards String: ";
String back;
int z=0;
for (int C=simstring.length()-1; C>=0; C--)
{
cout << simstring[C];
back[z]=simstring[C];
z++;
}

cout << "\n\n" << back;

}
I don't know what the type/interface of String looks like, but generally speaking you cannot use operator[] to access elements of a string that don't exist.

Presumably, String back; creates an empty string and it is still empty when back[z]=simstring[C]; happens, which means that z would be an invalid index.

With std::string it would look something like:
1
2
3
4
5
    std::string back ;
    for ( int C = simstring.length()-1; C>=0; --C )
    {
        back += simString[C] ;  // or back.append(1, simString[C]) ;
    }


Presumably your String has equivalent functionality.
That didn't work. Errors:

C:\Program Files\Microsoft Visual Studio\MyProjects\Palindrome\Palindrome.cpp(46) : error C2653: 'std' : is not a class or namespace name
C:\Program Files\Microsoft Visual Studio\MyProjects\Palindrome\Palindrome.cpp(46) : error C2065: 'string' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Palindrome\Palindrome.cpp(46) : error C2146: syntax error : missing ';' before identifier 'back'
C:\Program Files\Microsoft Visual Studio\MyProjects\Palindrome\Palindrome.cpp(49) : error C2065: 'simString' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Palindrome\Palindrome.cpp(49) : error C2109: subscript requires array or pointer type
That didn't work. Errors:


You would have to #include <string> for it to work.

It was meant as an example of functionality that is probably included in the non-standard string type you're using, which you need to familiarize yourself with if you want to avoid the error you're encountering.
That was just a guide, you'll need to use the appropriate functionality belonging to #include <lvp\string.h> - whatever that is.
Topic archived. No new replies allowed.