FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/strings/ReverseString.java at master · debugmm/Java · GitHub
debugmm
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
strings
/
ReverseString.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
41 lines (35 loc) · 959 Bytes
Breadcrumbs
Java
/
strings
/
ReverseString.java
Copy path
File metadata and controls
41 lines (35 loc) · 959 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
strings
;
/** Reverse String using different version */
public
class
ReverseString
{
public
static
void
main
(
String
[]
args
) {
assert
reverse
(
"abc123"
).
equals
(
"321cba"
);
assert
reverse2
(
"abc123"
).
equals
(
"321cba"
);
}
/**
* easiest way to reverses the string str and returns it
*
* @param str string to be reversed
* @return reversed string
*/
public
static
String
reverse
(
String
str
) {
return
new
StringBuilder
(
str
).
reverse
().
toString
();
}
/**
* second way to reverses the string str and returns it
*
* @param str string to be reversed
* @return reversed string
*/
public
static
String
reverse2
(
String
str
) {
if
(
str
==
null
||
str
.
isEmpty
()) {
return
str
;
}
char
[]
value
=
str
.
toCharArray
();
for
(
int
i
=
0
,
j
=
str
.
length
() -
1
;
i
<
j
;
i
++,
j
--) {
char
temp
=
value
[
i
];
value
[
i
] =
value
[
j
];
value
[
j
] =
temp
;
}
return
new
String
(
value
);
}
}
Back
|
FazBrowse Home
|
New Git URL