├── .idea
├── .gitignore
├── misc.xml
├── modules.xml
└── vcs.xml
├── JavaCodePractice.iml
├── README.md
├── out
└── production
│ └── JavaCodePractice
│ ├── ChangeSpecialCharacter.class
│ ├── ExtractLast4CharactersOfString.class
│ ├── FindFirstNonRepeatedCharacter.class
│ ├── FindLargestSmallestAnd2ndLargest.class
│ ├── FindMaximumDifference.class
│ ├── FindRepeatingElements.class
│ ├── MoveNegativeNumbersToStart.class
│ ├── PrintAllTheSubstrings.class
│ ├── PrintFirstLetterOfString.class
│ ├── PrintWordsVowelsFrequencyofeachCaracter.class
│ ├── RemoveAllSpecialCharacters.class
│ ├── RemoveDuplicateWordsFromSentence.class
│ ├── RemoveWhiteSpaces.class
│ ├── ReverseEachWord.class
│ ├── ReverseSentence.class
│ └── ReverseString.class
└── src
├── ChangeSpecialCharacter.java
├── ExtractLast4CharactersOfString.java
├── FindFirstNonRepeatedCharacter.java
├── FindLargestSmallestAnd2ndLargest.java
├── FindMaximumDifference.java
├── FindRepeatingElements.java
├── MoveNegativeNumbersToStart.java
├── PrintAllTheSubstrings.java
├── PrintFirstLetterOfString.java
├── PrintWordsVowelsFrequencyofeachCaracter.java
├── RemoveAllSpecialCharacters.java
├── RemoveDuplicateWordsFromSentence.java
├── RemoveWhiteSpaces.java
├── ReverseEachWord.java
├── ReverseSentence.java
└── ReverseString.java
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/JavaCodePractice.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | A collection of Java utility programs to demonstrate problem-solving techniques for strings, arrays, and basic programming challenges. Ideal for practice or as references for coding interviews.
2 |
3 | Skills Gained:
4 | - String Manipulation
5 | - Array Processing
6 | - Character Frequency Analysis
7 | - Handling Special Characters
8 | - Substring Generation
9 | - Sorting and Reordering Elements
10 | - Iterative Problem Solving
11 | - Debugging Java Programs
12 | - Optimizing Algorithms
13 | - Modular Code Design
14 |
15 | # Key Files and Descriptions
16 |
17 | - **`ReverseString.java`**: Reverses a given string character by character.
18 | Example: `Hello World` → `dlroW olleH`
19 |
20 | - **`ChangeSpecialCharacter.java`**: Replaces a specific special character with another.
21 | Example: `Hello @Japneet` → `Hello _Japneet`
22 |
23 | - **`ReverseSentence.java`**: Reverses the order of words in a sentence.
24 | Example: `Hello I am Japneet` → `Japneet am I Hello`
25 |
26 | - **`FindFirstNonRepeatedCharacter.java`**: Finds the first non-repeated character in a string.
27 | Example: `swiss` → `w`
28 |
29 | - **`RemoveAllSpecialCharacters.java`**: Removes all special characters from a string.
30 | Example: `Hello^^%#$(!)_+ J@apneet` → `Hello Japneet`
31 |
32 | - **`RemoveDuplicateWordsFromSentence.java`**: Removes duplicate words in a sentence.
33 | Example: `Hello I am Japneet Japneet am` → `Japneet am`
34 |
35 | - **`RemoveWhiteSpaces.java`**: Removes all white spaces from a string.
36 | Example: `Hello Japneet` → `HelloJapneet`
37 |
38 | - **`MoveNegativeNumbersToStart.java`**: Moves all negative numbers to the beginning of an array.
39 | Example: `[-1, 2, 3, -4, -7, 8]` → `[-1, -4, -7, 2, 3, 8]`
40 |
41 | - **`FindRepeatingElements.java`**: Identifies elements that repeat in an array and their counts.
42 | Example: `[4, 2, 3, 5, 1, 2, 4]` → `4: 2 times, 2: 2 times`
43 |
44 | - **`ExtractLast4CharactersOfString.java`**: Extracts the last 4 characters of a string.
45 | Example: `swiss` → `wiss`
46 |
47 | - **`FindMaximumDifference.java`**: Finds the maximum difference between the largest and smallest elements in an array.
48 | Example: `[10, 90, 2, 40, 1, 25]` → `89`
49 |
50 | - **`ReverseEachWord.java`**: Reverses each word in a string.
51 | Example: `Hello World` → `olleH dlroW`
52 |
53 | - **`PrintAllTheSubstrings.java`**: Prints all substrings of a string.
54 | Example: `abc` → `a, ab, abc, b, bc, c`
55 |
56 | - **`FindLargestSmallestAnd2ndLargest.java`**: Finds the largest, smallest, and second-largest numbers in an array.
57 | Example: `[23, 5, 1, 89, 65]` → `Largest: 89, 2nd Largest: 65, Smallest: 1`
58 |
59 | - **`PrintFirstLetterOfString.java`**: Extracts and prints the first letter of each word in a string.
60 | Example: `Hello I am Japneet` → `H I a J`
61 |
62 | - **`PrintWordsVowelsFrequencyofeachCharacter.java`**: Prints the total word count, count of vowels, and frequency of each character.
63 |
64 |
65 | 
66 |
67 |
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/ChangeSpecialCharacter.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/ChangeSpecialCharacter.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/ExtractLast4CharactersOfString.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/ExtractLast4CharactersOfString.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/FindFirstNonRepeatedCharacter.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/FindFirstNonRepeatedCharacter.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/FindLargestSmallestAnd2ndLargest.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/FindLargestSmallestAnd2ndLargest.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/FindMaximumDifference.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/FindMaximumDifference.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/FindRepeatingElements.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/FindRepeatingElements.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/MoveNegativeNumbersToStart.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/MoveNegativeNumbersToStart.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/PrintAllTheSubstrings.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/PrintAllTheSubstrings.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/PrintFirstLetterOfString.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/PrintFirstLetterOfString.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/PrintWordsVowelsFrequencyofeachCaracter.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/PrintWordsVowelsFrequencyofeachCaracter.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/RemoveAllSpecialCharacters.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/RemoveAllSpecialCharacters.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/RemoveDuplicateWordsFromSentence.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/RemoveDuplicateWordsFromSentence.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/RemoveWhiteSpaces.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/RemoveWhiteSpaces.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/ReverseEachWord.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/ReverseEachWord.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/ReverseSentence.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/ReverseSentence.class
--------------------------------------------------------------------------------
/out/production/JavaCodePractice/ReverseString.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JapneetSachdeva1/JavaCodingQuestionsPractice/f1cbe3320d82c309ced8d170496d0469d76808ee/out/production/JavaCodePractice/ReverseString.class
--------------------------------------------------------------------------------
/src/ChangeSpecialCharacter.java:
--------------------------------------------------------------------------------
1 | public class ChangeSpecialCharacter
2 | {
3 | public static void main (String[] args)
4 | {
5 | String str = "Hello @Japneet";
6 |
7 | System.out.print(printDifferentSpeicalCharacter(str));
8 | }
9 |
10 | private static String printDifferentSpeicalCharacter(String str)
11 | {
12 | return str.replace('@', '_');
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/ExtractLast4CharactersOfString.java:
--------------------------------------------------------------------------------
1 | public class ExtractLast4CharactersOfString
2 | {
3 | public static void main (String[] args)
4 | {
5 | String str = "swiss";
6 | printLast4Chars(str);
7 | }
8 |
9 | private static void printLast4Chars(String str)
10 | {
11 | int size = str.length();
12 | int counter = size-4;
13 | for(int i = counter; i map = new HashMap<>();
15 | int counter =1;
16 |
17 | for(int i=0; i entryMap: map.entrySet())
31 | {
32 | if(entryMap.getValue()==1)
33 | {
34 | System.out.print(entryMap.getKey());
35 | break;
36 | }
37 | }
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/src/FindLargestSmallestAnd2ndLargest.java:
--------------------------------------------------------------------------------
1 | public class FindLargestSmallestAnd2ndLargest
2 | {
3 | public static void main (String[] args)
4 | {
5 | int [] arr = {23,5,1,89,65,0,23,432,1};
6 | printLargest2ndLargestAndSmallest(arr);
7 | }
8 |
9 | private static void printLargest2ndLargestAndSmallest(int [] arr)
10 | {
11 | int lengthOfArray = arr.length;
12 | int largest = arr[0];
13 | int smallest = arr[0];
14 | int secondLargest = arr[0];
15 |
16 | for(int i=0; ilargest)
19 | {
20 | secondLargest = largest;
21 | largest = arr[i];
22 | }
23 |
24 | else if (arr[i] < smallest)
25 | {
26 | smallest = arr[i];
27 | }
28 |
29 | //System.out.println(largest+" "+secondLargest+" "+" "+smallest);
30 | }
31 |
32 | System.out.print(largest+" "+secondLargest+" "+" "+smallest);
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/FindMaximumDifference.java:
--------------------------------------------------------------------------------
1 | public class FindMaximumDifference {
2 | public static void main (String [] args)
3 | {
4 | int [] arr = {10,90,2,40,1,25};
5 | printMaximumDifference(arr);
6 | }
7 |
8 | private static void printMaximumDifference(int [] arr)
9 | {
10 | //Approach find greatest and smallest number in java and then subtract it.
11 | int max = arr[0];
12 | int min = arr[0];
13 |
14 | for(int i=0; i max)
17 | {
18 | max = arr[i];
19 | }
20 |
21 | else if (arr[i] < min)
22 | {
23 | min = arr[i];
24 | }
25 | }
26 |
27 | //Now once we get max and min from the array by iteration now we can perform the subtraction and //TODO: get Maximum Difference
28 |
29 | // int difference = max-min;
30 | // System.out.print("Max Difference is: "+difference);
31 | //or
32 | System.out.print("Max Difference is: "+ (max-min));
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/FindRepeatingElements.java:
--------------------------------------------------------------------------------
1 | import java.util.HashMap;
2 | import java.util.Map;
3 |
4 | public class FindRepeatingElements
5 | {
6 | public static void main (String[] args)
7 | {
8 | int [] arr = {4,2,3,5,1,2,4};
9 | printRepeatingElements(arr);
10 | }
11 |
12 | private static void printRepeatingElements(int[] arr)
13 | {
14 | Map map = new HashMap<>();
15 |
16 |
17 |
18 | for(int i = 0; i mapEntry : map.entrySet())
38 | {
39 | if(mapEntry.getValue()>1)
40 | {
41 | System.out.println("Key: "+mapEntry.getKey()+" Value:"+mapEntry.getValue());
42 | }
43 | }
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/src/MoveNegativeNumbersToStart.java:
--------------------------------------------------------------------------------
1 | import java.util.Arrays;
2 |
3 | public class MoveNegativeNumbersToStart
4 | {
5 | public static void main (String [] args)
6 | {
7 | int [] arr = {-1,2,3,-4,-7,8};
8 | moveNegativeNumbersTOStart(arr);
9 | }
10 |
11 | private static void moveNegativeNumbersTOStart(int [] arr) {
12 | int[] tempArr = new int[arr.length];
13 | int counter = 0;
14 | for (int i = 0; i < arr.length; i++) {
15 | if (arr[i] < 0) {
16 | //System.out.print(arr[i]);
17 | tempArr[counter] = arr[i];
18 | counter++;
19 | }
20 | }
21 |
22 | for (int i = 0; i < arr.length; i++)
23 | {
24 | if(arr[i]>0)
25 | {
26 | tempArr[counter] = arr[i];
27 | counter++;
28 | }
29 | }
30 |
31 | System.out.print(Arrays.toString(tempArr));
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/PrintAllTheSubstrings.java:
--------------------------------------------------------------------------------
1 | public class PrintAllTheSubstrings
2 | {
3 | public static void main (String [] args)
4 |
5 | {
6 | String str = "abc";
7 | printSubstrings(str);
8 | }
9 |
10 | private static void printSubstrings(String str)
11 | {
12 | for(int i=0; i freqMap = new HashMap<>();
36 |
37 | for(int i =0; i entry: freqMap.entrySet())
54 | {
55 | System.out.print(" Character: "+entry.getKey()+ " Frequency: "+entry.getValue());
56 | }
57 |
58 |
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/RemoveAllSpecialCharacters.java:
--------------------------------------------------------------------------------
1 | public class RemoveAllSpecialCharacters
2 | {
3 | public static void main (String[] args)
4 | {
5 | String str = "Hello^^%#$(!)_+ J@apneet";
6 | System.out.print(removeSpecialCharacters(str));
7 | }
8 |
9 | private static String removeSpecialCharacters(String str)
10 | {
11 | String newStr = str.replaceAll("[%^#$()!_+@]", "");
12 | return newStr;
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/src/RemoveDuplicateWordsFromSentence.java:
--------------------------------------------------------------------------------
1 | import java.util.HashMap;
2 | import java.util.Map;
3 |
4 | public class RemoveDuplicateWordsFromSentence
5 | {
6 | public static void main (String [] args)
7 | {
8 | String str = "Hello I am Japneet Japneet am";
9 | printSentenceWithoutDuplicates(str);
10 | }
11 |
12 | private static void printSentenceWithoutDuplicates(String str)
13 | {
14 | String [] strSplit = str.split("\\s");
15 | Map map = new HashMap<>();
16 | int counter = 1;
17 |
18 | for(int i=0; i entryMap: map.entrySet())
31 | {
32 | if(entryMap.getValue()>1)
33 | {
34 | System.out.print(entryMap.getKey()+" ");
35 | }
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/RemoveWhiteSpaces.java:
--------------------------------------------------------------------------------
1 | public class RemoveWhiteSpaces
2 | {
3 | public static void main (String[] args)
4 | {
5 | String str = " Hello Japne et ";
6 | System.out.print(removeWhiteSpace(str));
7 | }
8 |
9 | private static String removeWhiteSpace(String str)
10 | {
11 | String newStr = str.replaceAll("\\s", "");
12 | return newStr;
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/ReverseEachWord.java:
--------------------------------------------------------------------------------
1 | public class ReverseEachWord
2 | {
3 | public static void main (String [] args)
4 | {
5 | String str = "Hello World";
6 | printEachWordReverse(str);
7 |
8 | }
9 |
10 | private static void printEachWordReverse(String str)
11 | {
12 | //TODO: Also we can use Stacks for this programming question
13 |
14 | String [] arr = str.split(" ");
15 | String reverse = "";
16 | String reverseString = "";
17 |
18 | for(int i=0; i=0; j--)
23 | {
24 | reverse = String.valueOf(arr[i].charAt(j));
25 | System.out.print(reverse);
26 | reverseString += reverse;
27 | }
28 |
29 | System.out.print(" ");
30 | }
31 |
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/ReverseSentence.java:
--------------------------------------------------------------------------------
1 | public class ReverseSentence
2 | {
3 | public static void main (String[] args)
4 | {
5 | String str = "Hello I am Japneet";
6 | printReverseSentence(str);
7 | }
8 |
9 | private static void printReverseSentence(String str)
10 | {
11 | String [] strSplit = str.split("\\s");
12 |
13 | for(int i=strSplit.length-1; i>=0; i--)
14 | {
15 | System.out.print(strSplit[i]+" ");
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/ReverseString.java:
--------------------------------------------------------------------------------
1 | public class ReverseString
2 | {
3 | public static void main (String[] args)
4 | {
5 | String str = "Hello World";
6 | printReverseofString(str);
7 | }
8 |
9 | private static void printReverseofString (String str)
10 | {
11 | for(int i = str.length()-1; i>=0; i--)
12 | {
13 | System.out.print(str.charAt(i));
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------