Skip to content

Commit 0f6776a

Browse files
authored
Add files via upload
1 parent a8b0212 commit 0f6776a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Programs/SwitchingLetters.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.Scanner;
2+
3+
public class SwitchingLetters {
4+
5+
public static void main(String[] args) {
6+
7+
Scanner keyboard = new Scanner(System.in);
8+
9+
System.out.println("Please enter two numbers:");
10+
int index1 = keyboard.nextInt() - 1;
11+
int index2 = keyboard.nextInt() - 1;
12+
13+
keyboard.nextLine();
14+
System.out.println("Please enter a sentence:");
15+
String sentence = keyboard.nextLine();
16+
System.out.println("The new sentence:\n" + switchLetters(index1, index2, sentence));
17+
}
18+
19+
public static String switchLetters(int index1, int index2, String sentence) {
20+
return sentence.substring(0,index1) + sentence.charAt(index2) + sentence.substring(index1 + 1, index2) + sentence.charAt(index1) + sentence.substring(index2 + 1, sentence.length());
21+
22+
}
23+
}

0 commit comments

Comments
 (0)