From 01c68fe9ca2518d8fd05c4af04dbe42f60adca83 Mon Sep 17 00:00:00 2001 From: muarachmann Date: Mon, 7 Aug 2017 19:45:34 +0100 Subject: [PATCH 1/3] Add files via upload --- Main.java | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Main.java diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..6fa181e --- /dev/null +++ b/Main.java @@ -0,0 +1,85 @@ +package com.company; + +import java.util.Scanner; + +public class Main { + + public static void main(String[] args) { + String word; + int n; + String choice; + char temp; // variable to temporary store the + + Scanner input = new Scanner(System.in); + System.out.println("This program is to insert an asterisk(*) at the position indexed by the user in a particular string. "); + + System.out.println("Enter a string:"); + word = input.next(); + System.out.println("Enter the index(integer):"); + n = input.nextInt(); + + // Changing the character with an * using the charAt method to locate + // the character at that particular location + + temp = '*'; + + // strings are immutable thus we store each character of the string in an array so as to + // the individual characters + + char[] stringArray = new char[word.length()]; + + for (int i=0 ; i < stringArray.length ; i++){ + + stringArray[i] = word.charAt(i); + + if(i == n-1){ + stringArray[i] = temp; + } + } + + // reconverting array back to a string and storing it in the variable word. + + word = new String(stringArray); + System.out.println(word); + + // /*********************************************************/ + // EXTRA: Replacing a * at every index that is even / + // *********************************************************/ + System.out.println("Press yes or no to continue the program."); + choice = input.next(); + + switch (choice) { + + case "yes": { + System.out.println("\tThanks for continuing to use the program"); + System.out.println("\tThe program replaces an asterisk in every even index ;)."); + System.out.println("Enter a string:"); + word = input.next(); + + char[] newStringArray = new char[word.length()]; + + for (int i = 0; i < newStringArray.length; i++) { + + newStringArray[i] = word.charAt(i); + if (i % 2 == 0) { + newStringArray[i] = temp; + } + } + word = new String(newStringArray); + System.out.println(word); + + break; + } + + case "no":{ + System.out.println("Oops thanks for using the program. ;)."); + break; + } + + default: + System.out.println("Your request is invalid!\nThanks for using the Program"); + break; + } + + } +} From 811aa769a9877a1fc38f0697c8ddb5c485202099 Mon Sep 17 00:00:00 2001 From: muarachmann Date: Mon, 7 Aug 2017 19:48:19 +0100 Subject: [PATCH 2/3] Delete Main.java --- Main.java | 85 ------------------------------------------------------- 1 file changed, 85 deletions(-) delete mode 100644 Main.java diff --git a/Main.java b/Main.java deleted file mode 100644 index 6fa181e..0000000 --- a/Main.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.company; - -import java.util.Scanner; - -public class Main { - - public static void main(String[] args) { - String word; - int n; - String choice; - char temp; // variable to temporary store the - - Scanner input = new Scanner(System.in); - System.out.println("This program is to insert an asterisk(*) at the position indexed by the user in a particular string. "); - - System.out.println("Enter a string:"); - word = input.next(); - System.out.println("Enter the index(integer):"); - n = input.nextInt(); - - // Changing the character with an * using the charAt method to locate - // the character at that particular location - - temp = '*'; - - // strings are immutable thus we store each character of the string in an array so as to - // the individual characters - - char[] stringArray = new char[word.length()]; - - for (int i=0 ; i < stringArray.length ; i++){ - - stringArray[i] = word.charAt(i); - - if(i == n-1){ - stringArray[i] = temp; - } - } - - // reconverting array back to a string and storing it in the variable word. - - word = new String(stringArray); - System.out.println(word); - - // /*********************************************************/ - // EXTRA: Replacing a * at every index that is even / - // *********************************************************/ - System.out.println("Press yes or no to continue the program."); - choice = input.next(); - - switch (choice) { - - case "yes": { - System.out.println("\tThanks for continuing to use the program"); - System.out.println("\tThe program replaces an asterisk in every even index ;)."); - System.out.println("Enter a string:"); - word = input.next(); - - char[] newStringArray = new char[word.length()]; - - for (int i = 0; i < newStringArray.length; i++) { - - newStringArray[i] = word.charAt(i); - if (i % 2 == 0) { - newStringArray[i] = temp; - } - } - word = new String(newStringArray); - System.out.println(word); - - break; - } - - case "no":{ - System.out.println("Oops thanks for using the program. ;)."); - break; - } - - default: - System.out.println("Your request is invalid!\nThanks for using the Program"); - break; - } - - } -} From 173213d0250be37f137547ea991913d1a18bb9a0 Mon Sep 17 00:00:00 2001 From: muarachmann Date: Mon, 7 Aug 2017 19:49:53 +0100 Subject: [PATCH 3/3] CharacrterInsertion (*) Extra: Inserts character at all even positions in the string --- CharacterInsertion.java | 85 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 CharacterInsertion.java diff --git a/CharacterInsertion.java b/CharacterInsertion.java new file mode 100644 index 0000000..6fa181e --- /dev/null +++ b/CharacterInsertion.java @@ -0,0 +1,85 @@ +package com.company; + +import java.util.Scanner; + +public class Main { + + public static void main(String[] args) { + String word; + int n; + String choice; + char temp; // variable to temporary store the + + Scanner input = new Scanner(System.in); + System.out.println("This program is to insert an asterisk(*) at the position indexed by the user in a particular string. "); + + System.out.println("Enter a string:"); + word = input.next(); + System.out.println("Enter the index(integer):"); + n = input.nextInt(); + + // Changing the character with an * using the charAt method to locate + // the character at that particular location + + temp = '*'; + + // strings are immutable thus we store each character of the string in an array so as to + // the individual characters + + char[] stringArray = new char[word.length()]; + + for (int i=0 ; i < stringArray.length ; i++){ + + stringArray[i] = word.charAt(i); + + if(i == n-1){ + stringArray[i] = temp; + } + } + + // reconverting array back to a string and storing it in the variable word. + + word = new String(stringArray); + System.out.println(word); + + // /*********************************************************/ + // EXTRA: Replacing a * at every index that is even / + // *********************************************************/ + System.out.println("Press yes or no to continue the program."); + choice = input.next(); + + switch (choice) { + + case "yes": { + System.out.println("\tThanks for continuing to use the program"); + System.out.println("\tThe program replaces an asterisk in every even index ;)."); + System.out.println("Enter a string:"); + word = input.next(); + + char[] newStringArray = new char[word.length()]; + + for (int i = 0; i < newStringArray.length; i++) { + + newStringArray[i] = word.charAt(i); + if (i % 2 == 0) { + newStringArray[i] = temp; + } + } + word = new String(newStringArray); + System.out.println(word); + + break; + } + + case "no":{ + System.out.println("Oops thanks for using the program. ;)."); + break; + } + + default: + System.out.println("Your request is invalid!\nThanks for using the Program"); + break; + } + + } +}