converting to vector

Hi, I am struggling to convert my program to use vector instead of string, I would appreciate any help/guidance on how to do this. Thanks!

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <iomanip>
#include <cstdlib>	
#include <math.h>
#include <algorithm>
#include <string>
using namespace std;

string toUpper(string s1);

string removeNonAlphabets(string s1);

bool isPalindrome(string s1, int begins, int ends);

string toUpper(string s1)
{
	std::transform(s1.begin(), s1.end(), s1.begin(), ::toupper);

    return s1;
}

string removeNonAlphabets(string s1)
{
	for(string::iterator i = s1.begin(); i != s1.end(); i++)
   {
   		if(!isalpha(s1.at(i - s1.begin())))
			s1.erase(i);
   }
   return s1;
}

bool isPalindrome(string s1, int begins, int ends)
{
    if(begins == ends)
    	return true;
    else if(begins<ends)
    {
    	if(s1[begins]==s1[ends])
    		return isPalindrome(s1,begins+1,ends-1);
    	else
    		return false;
	}
	return false;
}

int main()
{
	string s1;
		
	cout << "Enter a word, phrase or sentence to see if it's a palindrome: " << endl;
	getline(cin,s1);
	
	cout << endl <<  "The original input string contains: " << endl;
	cout << s1 << endl;
	
	cout << endl << "After conversion to upper-case, input string becomes: " << endl;
	s1 = toUpper(s1);
	cout << s1 << endl;
	
	cout << endl << "After removal of non-alphabets, input string becomes: " << endl;
	s1 = removeNonAlphabets(s1);
	cout << s1 << endl;
		
	
	if(isPalindrome(s1,0,s1.size()-1))
	{
		cout << endl << s1 << " IS a palindrome!" << endl;
	}
	else
	{
		cout << endl << s1 << " is NOT  a palindrome!" << endl;
	}
	
	return 0;
}
I don't think this is the best idea, but I'm going to assume you have requirement.

Start by considering what kind of vector you should use; a vector of what type is going to be useful to replace a string?
yeah it is an assignment i managed to get it to work using vector string, but my palindrome function only returns true for some reason.

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

void loadVector(vector<string> &v);

void displayVector(vector<string> &v);

void toUpper(vector<string> &v);

void removeNonAlphabets(vector<string> &v);

bool isPalindrome(const vector<string> &v);

void loadVector(vector<string> &v)
{
	string input;
	
	cout << "Enter a word, phrase or sentence to see if it's a palindrome: " << endl;
	getline(cin, input);
	v.push_back(input);
    cout << endl;
}

void displayVector(vector<string> &v)
{
	for(int i = 0; i < v.size(); i++)
	{
		cout << v[i];
	}
}

void toUpper(vector<string> &v)
{
	for (int i = 0; i < v.size(); i++)
    {
        // Get the string
        string temp = v[i];


        // Loop through the string converting each char to uppercase
        int j = 0;

        while (temp[j]) 
		{ 
			temp[j] = toupper(temp[j]); 
			j++;
	    }

        // Store the string back into the vector.
        v[i] = temp;
    }
}

void removeNonAlphabets(vector<string> &v)
{
	for(int j = 0; j < v.size(); j++)
    {

        string& str = v[j];
        string temp;

        for(int m = 0; m < str.length(); m++)
        {
        	if (isalnum(str[m]))
         	{	
			   temp += str[m];
        	}
    
        }
        
		v[j] = temp;
    }
}

bool isPalindrome(const vector<string> &v)
{
	size_t i = 0;
    size_t j = v.size() - 1;
    
    while (i < j) 
	{
        if (v[i++] != v[j--]) 
		{
            return false;
        }
    }
    return true;
}

int main()
{
	vector<string> v;
	bool rs;
	
	loadVector(v);
		
	cout << endl <<  "The original input string contains: " << endl;
	displayVector(v);
	cout << endl << endl;	
	
	cout << "After conversion to upper-case, input string contains: " << endl;
    toUpper(v);
    displayVector(v);
    cout << endl << endl;
    
    cout << "After removal of non-alphabets, input string contains: " << endl;
    removeNonAlphabets(v);
    displayVector(v);
    cout << endl;
    
    rs = isPalindrome(v);
    cout << endl;	
	
	if(rs == true)
	{
		displayVector(v);
		cout << endl << " IS a palindrome!" << endl;
	}
	else
	{
		displayVector(v);
		cout << endl << " is NOT  a palindrome!" << endl;
	}
	
	return 0;
}
Topic archived. No new replies allowed.