From 7f3b60e9e3c6bc43bd5ec356b910f953c2735eb9 Mon Sep 17 00:00:00 2001 From: abhimanyu-stream Date: Tue, 24 Dec 2024 19:35:34 +0530 Subject: [PATCH] program added --- .../array/LongestPalindromicSubstring.java | 19 ++++++++++ .../FindIndexFirstOfWordOccurrenceString.java | 36 ++++++++++++++++++ .../com/stream/string/LengthOfLastWord.java | 25 +++++++++++++ .../stream/string/ReverseWordInString.java | 33 +++++++++++++++++ .../java/com/stream/string/WordSearch.java | 37 +++++++++++++++++++ 5 files changed, 150 insertions(+) create mode 100644 Array/array/src/main/java/com/stream/array/LongestPalindromicSubstring.java create mode 100644 String/string/src/main/java/com/stream/string/FindIndexFirstOfWordOccurrenceString.java create mode 100644 String/string/src/main/java/com/stream/string/LengthOfLastWord.java create mode 100644 String/string/src/main/java/com/stream/string/ReverseWordInString.java create mode 100644 String/string/src/main/java/com/stream/string/WordSearch.java diff --git a/Array/array/src/main/java/com/stream/array/LongestPalindromicSubstring.java b/Array/array/src/main/java/com/stream/array/LongestPalindromicSubstring.java new file mode 100644 index 0000000..a535903 --- /dev/null +++ b/Array/array/src/main/java/com/stream/array/LongestPalindromicSubstring.java @@ -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 + + } +} diff --git a/String/string/src/main/java/com/stream/string/FindIndexFirstOfWordOccurrenceString.java b/String/string/src/main/java/com/stream/string/FindIndexFirstOfWordOccurrenceString.java new file mode 100644 index 0000000..cef0594 --- /dev/null +++ b/String/string/src/main/java/com/stream/string/FindIndexFirstOfWordOccurrenceString.java @@ -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 + } + +} diff --git a/String/string/src/main/java/com/stream/string/LengthOfLastWord.java b/String/string/src/main/java/com/stream/string/LengthOfLastWord.java new file mode 100644 index 0000000..2b974ec --- /dev/null +++ b/String/string/src/main/java/com/stream/string/LengthOfLastWord.java @@ -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; + } + +} diff --git a/String/string/src/main/java/com/stream/string/ReverseWordInString.java b/String/string/src/main/java/com/stream/string/ReverseWordInString.java new file mode 100644 index 0000000..c630d8a --- /dev/null +++ b/String/string/src/main/java/com/stream/string/ReverseWordInString.java @@ -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(); + } +} diff --git a/String/string/src/main/java/com/stream/string/WordSearch.java b/String/string/src/main/java/com/stream/string/WordSearch.java new file mode 100644 index 0000000..34d46b0 --- /dev/null +++ b/String/string/src/main/java/com/stream/string/WordSearch.java @@ -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. + */ \ No newline at end of file