0% found this document useful (0 votes)
9 views

Wipro TalentNext Java Full Stack

The document contains multiple Java programs demonstrating various string manipulation techniques, including checking for palindromes, concatenating strings, repeating characters, and removing specific characters. Each program is structured with a main method and relevant functions to perform the specified operations. The code examples are designed for hands-on assignments in a Java Full Stack course.

Uploaded by

Yehaa Km
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Wipro TalentNext Java Full Stack

The document contains multiple Java programs demonstrating various string manipulation techniques, including checking for palindromes, concatenating strings, repeating characters, and removing specific characters. Each program is structured with a main method and relevant functions to perform the specified operations. The code examples are designed for hands-on assignments in a Java Full Stack course.

Uploaded by

Yehaa Km
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Wipro TalentNext Java Full Stack

Java Fundamentals Hands-on Assignment-2


Date:23-07-2024
BY,Yehaasary KM-IIIrd yr AI&DS
1) public class PalindromeString

public static void main(String[] args) {

String string = "Madam";

boolean flag = true;

string = string.toLowerCase();

for(int i = 0; i < string.length()/2; i++){

if(string.charAt(i) != string.charAt(string.length()-i-1)){

flag = false;

break;

if(flag)

System.out.println("Given string is palindrome");

else

System.out.println("Given string is not a palindrome");

}
2) import java.util.Scanner;

public class ConcatenateStrings {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first string: ");

String str1 = scanner.nextLine();

System.out.print("Enter the second string: ");

String str2 = scanner.nextLine();

String result = concatenateStrings(str1, str2);

System.out.println("Result: " + result);

public static String concatenateStrings(String str1, String str2) {

StringBuilder sb = new StringBuilder();

int i = 0;

int j = 0;

while (i < str1.length() || j < str2.length()) {

if (i < str1.length()) {
sb.append(str1.charAt(i));

i++;

if (j < str2.length()) {

sb.append(str2.charAt(j));

j++;

return sb.toString().toLowerCase();

3) public class Main {

public static void main(String[] args) {

String input = "Wipro";

String result = repeatFirstTwoChars(input);

System.out.println(result);

public static String repeatFirstTwoChars(String str) {

int n = str.length();

if (n < 2) {
return "";

String firstTwoChars = str.substring(0, 2);

StringBuilder result = new StringBuilder();

for (int i = 0; i < n; i++) {

result.append(firstTwoChars);

return result.toString();

4) public class Main {

public static void main(String[] args) {

String input1 = "TomCat";

String input2 = "Apron";

System.out.println(getFirstHalfIfEven(input1));

System.out.println(getFirstHalfIfEven(input2));

public static String getFirstHalfIfEven(String str) {

int length = str.length();


if (length % 2 != 0) {

return null;

return str.substring(0, length / 2);

5) public class Main {

public static void main(String[] args) {

String input = "Suman";

String result = removeFirstAndLastChar(input);

System.out.println(result);

public static String removeFirstAndLastChar(String str) {

if (str.length() <= 2) {

return "";

return str.substring(1, str.length() - 1);

}
6) public class ShortLongShort {

public static String shortLongShort(String a, String b) {

String shortStr = (a.length() < b.length()) ? a : b;

String longStr = (a.length() < b.length()) ? b : a;

return shortStr + longStr + shortStr;

public static void main(String[] args) {

String result1 = shortLongShort("hi", "hello");

System.out.println(result1);

String result2 = shortLongShort("hello", "hi");

System.out.println(result2);

String result3 = shortLongShort("", "world");

System.out.println(result3);

}
7)

public class RemoveX {

public static String removeX(String str) {

if (str.length() > 0) {

if (str.startsWith("x")) {

str = str.substring(1);

if (str.endsWith("x")) {

str = str.substring(0, str.length() - 1);

return str;

public static void main(String[] args) {

String result1 = removeX("xHix");

System.out.println(result1);

String result2 = removeX("America");

System.out.println(result2);
String result3 = removeX("xHellox");

System.out.println(result3);

String result4 = removeX("x");

System.out.println(result4);

String result5 = removeX("");

System.out.println(result5);

8) public class RemoveStarAndNeighbors {

public static String removeStarAndNeighbors(String str) {

StringBuilder result = new StringBuilder();

int length = str.length();

for (int i = 0; i < length; i++) {

if (str.charAt(i) == '*') {

i += 1;

} else if (i > 0 && str.charAt(i - 1) == '*') {

continue;

} else if (i < length - 1 && str.charAt(i + 1) == '*') {

continue;
} else {

result.append(str.charAt(i));

return result.toString();

public static void main(String[] args) {

String result1 = removeStarAndNeighbors("ab*cd");

System.out.println(result1);

String result2 = removeStarAndNeighbors("a*b*c");

System.out.println(result2);

String result3 = removeStarAndNeighbors("ab**cd");

System.out.println(result3);

String result4 = removeStarAndNeighbors("**ab**cd**");

System.out.println(result4);

String result5 = removeStarAndNeighbors("ab*cd*ef");

System.out.println(result5);

}
9) import java.util.Scanner;

public class AlternateCharacters {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first string: ");

String a = scanner.nextLine();

System.out.print("Enter the second string: ");

String b = scanner.nextLine();

String result = alternateCharacters(a, b);

System.out.println("Output: " + result);

scanner.close();

public static String alternateCharacters(String a, String b) {

StringBuilder result = new StringBuilder();

int minLength = Math.min(a.length(), b.length());

for (int i = 0; i < minLength; i++) {


result.append(a.charAt(i));

result.append(b.charAt(i));

if (a.length() > minLength) {

result.append(a.substring(minLength));

if (b.length() > minLength) {

result.append(b.substring(minLength));

return result.toString();

10) import java.util.Scanner;

class RepeatLastNChar{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a string: ");

String input = scanner.nextLine();

System.out.print("Enter an integer n: ");

int n = scanner.nextInt();

String result = repeatLastNChars(input, n);


System.out.println("Output: " + result);

scanner.close();

public static String repeatLastNChars(String str, int n) {

String lastNChars = str.substring(str.length() - n);

StringBuilder result = new StringBuilder();

for (int i = 0; i < n; i++) {

result.append(lastNChars);

return result.toString();

You might also like