Skip to content

Commit

Permalink
program added
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyu-stream committed Dec 24, 2024
1 parent 9a5d322 commit 7f3b60e
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
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

}
}
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
}

}
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;
}

}
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 String/string/src/main/java/com/stream/string/WordSearch.java
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.
*/

0 comments on commit 7f3b60e

Please sign in to comment.