Palindrome Program Issues

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//I can't see any issues with the way I was using to define String back as String 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.
//I definitely need to create a new string which is equal to the original backwards. It just won't let me


/* 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--)
{
back[z]=simstring[C];
z++;
}

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

}
Last edited on
line 53 you're writing in uninitialized memory.
Replace back[z]=simstring[C]; z++; with back.push_back( simstring[C] );
What does this push_back operator do? We've never learned it. Also I just tried this and I got an error saying push back is not a member of String
Last edited on
First off, use:
1
2
#include <iostream>
#include <string> 


Some info on this:
http://members.gamedev.net/sicrane/articles/iostream.html

Also, you declare your strings as String. Use lower case string.

.push_back() will add an element to the end of the string
Last edited on
Also, another option( to add to Cire's post, in your duplicate thread ).

You can re-size the string, to the size of the string you wish to copy in to it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string input;

std::cout << "Please enter some text: ";
std::getline( std::cin, input, '\n' );

std::string copy;

// Change the length of copy and fill with '+'.
copy.resize( input.length(), '+' );

// The above is for a visual reference.
// You can just pass .resize() the value you wish
// to resize too, as the statement below:
/*  copy.resize( input.length() );  */

std::cout << "text: " << input << '\n';
std::cout << "copy: " << copy << '\n';


output:
Please enter some text: hi there
text: hi there
copy: ++++++++
Last edited on
Yeah, I can't use the <string> library for whatever reason, our school issued laptops can only use the <lvp\string.h> library
Topic archived. No new replies allowed.