/
StringManip.cs
206 lines (175 loc) · 6.71 KB
/
StringManip.cs
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StringManipulation
{
class Program
{
static void Main(string[] args)
{
//This is a program written for the portfolio of Chris Jones. The purpose of this program is to read in a string and ask for a user input to do a certain action.
Console.WriteLine("Please enter a string to manipulate: ");
string str = Console.ReadLine();
//Default answer = -1, different from all the other answers.
int answer = -1;
while (answer != 0)
{
Console.WriteLine("\n\nHere are the following options to choose:");
Console.WriteLine("1. Print out all permutations of the string.");
Console.WriteLine("2. Print out the score that string would get in a game of scrabble.");
Console.WriteLine("3. Reverse the order of the string");
Console.WriteLine("4. Check if the string is a palindrome.");
Console.WriteLine("5. Different string.");
Console.WriteLine("0. Exit program.");
//Read in answer. If it's not an integer, use answer = -1.
string temp = Console.ReadLine();
if (!int.TryParse(temp, out answer))
answer = -1;
Console.WriteLine(" ");
switch (answer)
{
case 1:
perm(str);
break;
case 2:
scrabble(str);
break;
case 3:
reverse(str);
break;
case 4:
palin(str);
break;
case 5:
//New string
Console.WriteLine("Please enter a new string:");
str = Console.ReadLine();
break;
case 0:
break;
default:
//Error check for answer
Console.WriteLine("This is not a valid response. Please try again.\n");
break;
}
}
}
//Called function
static void perm(string a)
{
char[] c = a.ToCharArray();
if (c.Length > 5)
{
Console.WriteLine("WARNING: This is a long string and will take a long time to permute. Continune? y/n:");
char yn = (char)Console.Read();
if (yn == 'n')
return;
}
setper(c);
}
//Actual permutating recursive functions
static void swap(ref char a, ref char b)
{
if (a == b) return;
//a = a XOR b
a ^= b;
//b = b XOR a
b ^= a;
a ^= b;
}
static void setper(char[] list)
{
int x=list.Length-1;
go(list,0,x);
}
static void go (char[] list, int k, int m)
{
int i;
if (k == m)
{
Console.WriteLine (list);
}
else
for (i = k; i <= m; i++)
{
swap (ref list[k],ref list[i]);
go (list, k+1, m);
swap (ref list[k],ref list[i]);
}
}
static void scrabble(string a)
{
a = a.ToUpper(); //Make sure all characters are upper-cased.
char[] c = a.ToCharArray();
int total = 0;
for (int i = 0; i < c.Length; i++)
{
total += checkScrab(c[i]);
}
Console.WriteLine("The string " + a + " would be worth " + total + " points in Scrabble.\n\n\n");
}
static int checkScrab(char q)
{
int ans = 0;
/* A 26-case switch could be used here, but that would be inefficient to write.*/
char[] One = new char[] { 'E', 'A', 'I', 'O', 'N', 'R', 'T', 'L', 'S', 'U' };
char[] Two = new char[] { 'D', 'G' };
char[] Three = new char[] { 'B', 'C', 'M', 'P' };
char[] Four = new char[] { 'F', 'H', 'V', 'W', 'Y' };
char[] Five = new char[] { 'K' };
char[] Eight = new char[] { 'J', 'X' };
char[] Ten = new char[] { 'Q', 'Z' };
if (One.Contains(q))
ans = 1;
else if (Two.Contains(q))
ans = 2;
else if (Three.Contains(q))
ans = 3;
else if (Four.Contains(q))
ans = 4;
else if (Five.Contains(q))
ans = 5;
else if (Eight.Contains(q))
ans = 8;
else if (Ten.Contains(q))
ans = 10;
return ans;
}
static void reverse(string a)
{
char[] c = a.ToCharArray();
char[] crev = new char[c.Length];
for (int i = 0; i < c.Length; i++)
{
crev[i] = c[c.Length - 1 - i];
}
string b = new string(crev);
Console.WriteLine("The reverse of " + a + " is " + b + "\n\n\n");
}
static void palin(string a)
{
a = a.ToUpper();
char[] c = a.ToCharArray();
bool answer = true;
int len = c.Length;
//the words "a" and "as" are palindromes
if (len < 3)
{
Console.WriteLine("The word " + a + " is a palindrome because it only has 1 or 2 letters.\n\n\n");
return;
}
/*By looking at the pseudocode, it can be seen that it does not matter if the string length is even or odd*/
int midAR = len / 2;
for (int i = 0; i < midAR; i++)
{
if (!(c[i] == c[len - 1 - i])) //for AABBAA, check index 0 and 5, then check 1 and 4, then 2 and 3.
answer = false; //If they are not the same at any point, answer = false.
}
if (answer)
Console.WriteLine("The word " + a + " is a paldindrome!\n\n\n");
else
Console.WriteLine("The word " + a + " is NOT a paldindrome!\n\n\n");
}
}
}