-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a5d322
commit 7f3b60e
Showing
5 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
Array/array/src/main/java/com/stream/array/LongestPalindromicSubstring.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.stream.array; | ||
|
||
public class LongestPalindromicSubstring { | ||
|
||
public static void main(String[] args) { | ||
|
||
String str ="racecar"; | ||
|
||
String strReversed = new StringBuilder(str).reverse().toString(); | ||
if(str.equals(strReversed)){ | ||
System.out.println(str + " is palindrome"); | ||
}else{ | ||
System.out.println(str + "is not palindorme"); | ||
} | ||
|
||
// Find Longest Palindromic Substring | ||
|
||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
String/string/src/main/java/com/stream/string/FindIndexFirstOfWordOccurrenceString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.stream.string; | ||
|
||
import java.util.Arrays; | ||
|
||
public class FindIndexFirstOfWordOccurrenceString { | ||
|
||
public static void main(String[] args) { | ||
String input = "as a career path java programming language should be selected by fresher. insteadof java a fresher should choose c c++ javascript python as a programming language"; | ||
|
||
|
||
// Split by whitespace and '.' | ||
String[] result = input.split("\\s+|\\."); | ||
Arrays.stream(result).filter(f->!f.isEmpty()).forEach(System.out::println);// No whitespace in output | ||
|
||
String toFindIndexOf = "programming"; | ||
|
||
int index = findIndexOfWordInString(result, toFindIndexOf); | ||
if(index >=0){ | ||
System.out.println(toFindIndexOf+" firstly found at position " + index); | ||
}else{ | ||
System.out.println("Not found"); | ||
} | ||
} | ||
|
||
private static int findIndexOfWordInString(String[] input, String toFindIndexOf) { | ||
|
||
for(int i = 0; i < input.length; i++){ | ||
|
||
if(toFindIndexOf.equals(input[i])) | ||
return i; | ||
|
||
} | ||
return -1;// Not found in Array | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
String/string/src/main/java/com/stream/string/LengthOfLastWord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.stream.string; | ||
|
||
public class LengthOfLastWord { | ||
public static void main(String[] args) { | ||
|
||
String input = "as a career path java programming language should be selected by fresher"; | ||
|
||
String[] str = input.split(" "); | ||
|
||
|
||
int length = findLengthOfLastWordInString(str); | ||
System.out.println(length); | ||
|
||
} | ||
|
||
private static int findLengthOfLastWordInString(String[] str) { | ||
int length = 0; | ||
for(int i = str.length - 1; i >=0;i--){ | ||
length = str[i].length(); | ||
break; | ||
} | ||
return length; | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
String/string/src/main/java/com/stream/string/ReverseWordInString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.stream.string; | ||
|
||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
|
||
public class ReverseWordInString { | ||
|
||
public static void main(String[] args) { | ||
// Input string | ||
String input = "Hello World from Java"; | ||
//System.out.println("Original String: " + input); | ||
|
||
// Call the method to reverse words | ||
String reversed = reverseWords(input); | ||
System.out.println("Reversed Words: " + reversed); | ||
} | ||
|
||
private static String reverseWords(String input) { | ||
|
||
String[] str = input.split(" "); | ||
|
||
|
||
//Arrays.stream(str).forEach(System.out::println); | ||
StringBuilder builder = new StringBuilder(); | ||
for(int i = str.length - 1; i >=0; i--){ | ||
builder.append(str[i]).append(" "); | ||
} | ||
//System.out.println(builder.toString()); | ||
|
||
|
||
return builder.toString(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
String/string/src/main/java/com/stream/string/WordSearch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.stream.string; | ||
|
||
public class WordSearch { | ||
|
||
public static void main(String[] args) { | ||
|
||
//String[] str = new String[]{"as a career path java programming langugae should be selected by fresher"}; | ||
String input = "as a career path java programming language should be selected by fresher"; | ||
|
||
String[] str = input.split(" "); | ||
String toSearch = "java"; | ||
String result = searchWordInStr(str, toSearch); | ||
System.out.println(result); | ||
} | ||
|
||
private static String searchWordInStr(String[] str, String toSearch) { | ||
String foundMessage = null; | ||
for(int i = 0; i < str.length; i++){ | ||
if(toSearch.equals(str[i])) { | ||
return foundMessage = "at position " + i + " word " + toSearch + " is found"; | ||
} | ||
|
||
} | ||
return "Not found"; // Return this only if the word is not found | ||
|
||
|
||
} | ||
} | ||
|
||
|
||
/** | ||
* for(int i = 0; i < str.length - 1; i++){} | ||
* Your program has a logical flaw in the loop inside the searchWordInStr method. Here are the key issues: | ||
* | ||
* Overwriting foundMessage: Inside the loop, foundMessage is overwritten during every iteration, even if the word has already been found. This causes the program to potentially return "Not found" even if the word exists in the array. | ||
* Loop Bound Issue: The condition i < str.length - 1 excludes the last element of the str array, so it won't check if the word is present at the last position. | ||
*/ |