-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyString.java
More file actions
186 lines (163 loc) · 5.63 KB
/
MyString.java
File metadata and controls
186 lines (163 loc) · 5.63 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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Task_4;
/**
* Class MyString contains string manipulative functions.
* @author Maryam Ejaz
*/
public class MyString{
public String inputString;
public MyString(String inputString){
this.inputString = inputString;
}
/*
* This function removes spaces from the string
* @returns String
*/
public String removeSpaces(){
String StrwithoutSpaces;
//Returns size exluding spaces.
int size = this.inputString.length()- this.numberOfSpaces();
char[] array = new char[size];
//Exclusion of spaces from a string
for(int y=0, x=0; y<this.inputString.length(); y++){
if (this.inputString.charAt(y) == ' '){
//ignores space
}
else{
array[x] = this.inputString.charAt(y);
x++;
}
}
StrwithoutSpaces = String.copyValueOf(array);
return StrwithoutSpaces;
}
/*
* This function returns number of spaces in a string.
* @returns int
*/
public int numberOfSpaces(){
int space = 0;
for(int i=0; i<this.inputString.length(); i++){
if (this.inputString.charAt(i) == ' '){
space++;
}
}
return space;
}
/*
* This function converts first letter of each word into upper case.
* @returns String
*/
public String FirstCharUpperCase(){
char ch;
char[] array = new char[inputString.length()];
array = inputString.toCharArray();
for(int y=0; y<this.inputString.length(); y++){
if(y == 0 || inputString.charAt(y-1) == ' '){
if (this.inputString.charAt(y) >= 'a' && this.inputString.charAt(y) <= 'z') {
ch = (char)(this.inputString.charAt(y) - 32);
array[y] = ch;
}
}
}
inputString = String.copyValueOf(array);
return inputString;
}
/*
* This function removes special symbols from the string
* @returns String
*/
public String removeSpecialSymbols(){
String StrwithoutSymbols;
//Returns size exluding special symbols.
int size = this.inputString.length()- this.numberOfSpecialSymbols();
char[] array = new char[size];
//Exclusion of special symbols from a string
for(int y=0, x=0; y<this.inputString.length(); y++){
if ((this.inputString.charAt(y) >= '!' && this.inputString.charAt(y) <= '/') ||
(this.inputString.charAt(y) >= ':' && this.inputString.charAt(y) <= '@') ||
(this.inputString.charAt(y) >= '[' && this.inputString.charAt(y) <= '`') ||
(this.inputString.charAt(y) >= '{' && this.inputString.charAt(y) <= '~')){
//ignores special symbols
}
else{
array[x] = this.inputString.charAt(y);
x++;
}
}
StrwithoutSymbols = String.copyValueOf(array);
return StrwithoutSymbols;
}
/*
* This function returns number of special symbols in a string.
* @returns int
*/
public int numberOfSpecialSymbols(){
int sym = 0;
for(int i=0; i<this.inputString.length(); i++){
if ((this.inputString.charAt(i) >= '!' && this.inputString.charAt(i) <= '/') ||
(this.inputString.charAt(i) >= ':' && this.inputString.charAt(i) <= '@') ||
(this.inputString.charAt(i) >= '[' && this.inputString.charAt(i) <= '`') ||
(this.inputString.charAt(i) >= '{' && this.inputString.charAt(i) <= '~')){
sym++;
}
}
return sym;
}
/*
* This function returns number of occurances of specified character in a string.
* @returns int
*/
public int numberOfOccurences(char ch){
int occur = 0;
for(int i=0; i<this.inputString.length(); i++){
if (this.inputString.charAt(i) == ch){
occur++;
}
}
return occur;
}
/*
* This function returns string without repeated characters.
* @returns int
*/
public String removeRepeatedCharacters(){
int[] array = new int[inputString.length()];
int repeated = 0;
int size;
String output;
for(int n=0; n<inputString.length(); n++ ){
array[n] = -1;
}
//finds total no. of repeated characters.
for(int i=0; i<this.inputString.length(); i++){
for (int y=i+1; y<this.inputString.length(); y++){
if (this.inputString.charAt(i) == this.inputString.charAt(y)){
if (array[y] >= -1) {
array[i]++;
array[y]--;
repeated++;
}
}
}
}
//excludes repeated characters.
size = inputString.length() - repeated;
char[] arrayCh = new char [size];
for(int y=0, x=0; y<this.inputString.length(); y++){
if (array[y] >= -1){
arrayCh[x] = inputString.charAt(y);
x++;
}
else{
//ignores repeated ones
}
}
output = String.copyValueOf(arrayCh);
return output;
}
}