-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringReverse.java
More file actions
43 lines (36 loc) · 1.04 KB
/
Copy pathStringReverse.java
File metadata and controls
43 lines (36 loc) · 1.04 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
import java.util.Scanner;
public class StringReverse {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter a string: ");
String s= sc.nextLine();
//PRINTING ORIGINAL STRING
char c[]= s.toCharArray();
for(int i=0; i<c.length; i++){
System.out.print(c[i]);
}
System.out.println();
//METHOD 1: REVERSE STRING
String str="";
char ch;
for(int i=c.length-1; i>=0; i--){
ch= s.charAt(i);
str+=ch;
}
System.out.println(str);
//METHOD2: REVERSE STRING
String str2="";
char c2[]= new char[c.length];
for(int i=0; i<c2.length; i++){
c2[i]= c[c.length-i-1];
str2= str2 + c2[i];
}
System.out.println(str2);
//COUNT CHARACTERS IN A STRING
int count=0;
for(int i=0; i<c.length; i++){
count++;
}
System.out.println("String length: " + count);
}
}