-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringStudy.cpp
More file actions
153 lines (111 loc) · 2.99 KB
/
stringStudy.cpp
File metadata and controls
153 lines (111 loc) · 2.99 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*#########################################################
Name: Jesse Goodspeed
Title: Strings
Description: String program uses functions to Reverse a string, check if its a palindrome and output all Substrings of even length
Date: April 24th, 2015
####################################################*/
#include <iostream>
#include <string>
using namespace std;
//function declarations
//Pre-condition: a given string
//Post-condition: reverses the elements in the given string
string reverse(string s1);
//Pre-condition: a given string
//Post-condition: checks string to see if it is identical to original string after it is reversed
bool isPalindrome(string s1);
//Pre-condition: a given string
//Post-condition: prints all the even-lengthed substrings of the words in the given string
void allEvenSubstr(string s1);
int main(){
string input;
char space = ' ';
cout<<"Enter an input string ( word / sentence )"<<endl;
getline(cin, input);
//Call the reverse string function
cout<<"The reverse of the string is:"<< reverse(input)<<endl;
//Check if it is a palindrome
if( isPalindrome(input) )
{
cout<<"The input string is a palindrome!\n";
}
else
{
cout<<"The input string is not a Palindrome!\n";
}
//Output even length subtrings of the input string
cout<<"Even Length Substrings of the input strings are:\n";
allEvenSubstr(input);
cout<<endl;
return 0;
}
//function definitions
//reverse-string function
string reverse(string s1)
{
string temp;
//for-loop to initialize temp variable with reversed string
for(int i=0; i<s1.length(); i++)
{
temp[i] = s1[s1.length()-1-i];
}
//2nd for-loop to assign string with new temp variable (aka reversed string)
for(int i=0; i<s1.length(); i++)
{
s1[i] = temp[i];
}
return s1;
}
//palindrome-test function
bool isPalindrome(string s1)
{
int h = s1.length();
char space = ' '; //target
//for-loop to erase all spaces
for(int i = 0; i<(h-1); i++)
{
if(s1[i] == space)
s1.erase(i, 1);
}
//because spaces were removed from the string - the string length has changed
h = s1.length();
//test for palindrome
for(int i=0; i<(h-1); i++)
{
if(s1[i] != s1[h-i-1])
return false;
}
return true;
}
//even-substring function
void allEvenSubstr(string s1)
{
int h = s1.length();
int gap, anothaGap, start;
char space = ' ';
for(int i=0; i<h; i++)
{
//if-statement prevents substrings that begin with a space
if(s1[i] != space)
{
for(int j=2; j<h-1; j = j+2)
{
gap = s1.find(space,i); //searches for spaces and returns their positions in string
//if-statement prevents including spaces within a substring
if ((j+i)<=gap)
{
string temp = s1.substr(i, j);
cout<< temp << endl;
}
/*else-if statement prevents the end of a string from being truncated,
while printing substrings - STILL BUGGY - will print multiple odd-length substrings
at tail-end of string*/
else if (gap <= 0)
{
string temp = s1.substr(i, j);
cout<< temp << endl;
}
}
}
}
}