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

labbb

The document contains multiple Java programs that perform various string manipulations. These include converting lowercase to uppercase, checking for palindromes, splitting strings, reversing strings, counting vowels and consonants for a treasure chest lock, reversing words in a sentence, concatenating strings without common characters, alternating capitalization, and checking for strong passwords. Each program prompts the user for input and outputs the result based on the specified functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

labbb

The document contains multiple Java programs that perform various string manipulations. These include converting lowercase to uppercase, checking for palindromes, splitting strings, reversing strings, counting vowels and consonants for a treasure chest lock, reversing words in a sentence, concatenating strings without common characters, alternating capitalization, and checking for strong passwords. Each program prompts the user for input and outputs the result based on the specified functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#TASK1

import java.util.Scanner;
public class LowerToUpperConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a string:");
String input = scanner.nextLine();

String result = "";

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


char ch = input.charAt(i);

if (ch >= 'a' && ch <= 'z') {


result += (char) (ch - 32);
} else {
result += ch;
}
}

System.out.println("Converted string:");
System.out.println(result);

scanner.close();
}
}

#TASK2
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a string:");
String input = scanner.nextLine();

boolean isPalindrome = true;

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


if (input.charAt(i) != input.charAt(input.length() - 1 - i)) {
isPalindrome = false;
break;
}
}

System.out.println(isPalindrome);

scanner.close();
}
}

#TASK3
import java.util.Scanner;
public class StringSplitter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter the string:");


String inputString = scanner.nextLine();

System.out.println("Enter the split character:");


char splitChar = scanner.next().charAt(0);

String currentPart = "";

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


char ch = inputString.charAt(i);

if (ch == splitChar) {
System.out.println(currentPart);
currentPart = "";
} else {
currentPart += ch;
}
}

if (!currentPart.isEmpty()) {
System.out.println(currentPart);
}

scanner.close();
}
}

#TASK4
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a string:");
String input = scanner.nextLine();

String reversed = "";

for (int i = input.length() - 1; i >= 0; i--) {


reversed += input.charAt(i);
}

System.out.println("Reversed string:");
System.out.println(reversed);

scanner.close();
}
}

#TASK5
import java.util.Scanner;
public class TreasureChestLock {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter the phrase:");


String phrase = scanner.nextLine();

int vowelCount = 0;
int consonantCount = 0;
for (int i = 0; i < phrase.length(); i++) {
char ch = Character.toLowerCase(phrase.charAt(i));

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {


vowelCount++;
} else if (ch >= 'a' && ch <= 'z') {
consonantCount++;
}
}

if (vowelCount > 0 && consonantCount > 0 && vowelCount % 3 == 0 &&


consonantCount % 5 == 0) {
System.out.println("Aaarr! Me Plunder!!");
} else {
System.out.println("Blimey! No Plunder!!");
}

scanner.close();
}
}

#TASK6
import java.util.Scanner;
public class ReverseWords {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a sentence:");
String input = scanner.nextLine();

String[] words = input.split(" ");

StringBuilder reversedSentence = new StringBuilder();

for (int i = words.length - 1; i >= 0; i--) {


reversedSentence.append(words[i]);
if (i > 0) {
reversedSentence.append(" ");
}
}

System.out.println("Reversed sentence: " + reversedSentence.toString());


}
}

#TASK7
import java.util.Scanner;
import java.util.HashSet;

public class ConcatenateWithoutCommon {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

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


String str1 = scanner.nextLine();

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


String str2 = scanner.nextLine();
HashSet<Character> commonChars = new HashSet<>();

for (char c : str1.toCharArray()) {


if (str2.indexOf(c) != -1) {
commonChars.add(c);
}
}

StringBuilder result = new StringBuilder();


for (char c : str1.toCharArray()) {
if (!commonChars.contains(c)) {
result.append(c);
}
}

for (char c : str2.toCharArray()) {


if (!commonChars.contains(c)) {
result.append(c);
}
}

System.out.println("Modified string: " + result.toString().toUpperCase());


}
}

#TASK8
import java.util.Scanner;
public class AlternatingCaps {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a sentence:");
String input = scanner.nextLine();

StringBuilder result = new StringBuilder();


boolean isLower = true;

for (char c : input.toCharArray()) {


if (Character.isLetter(c)) {
if (isLower) {
result.append(Character.toLowerCase(c));
} else {
result.append(Character.toUpperCase(c));
}
isLower = !isLower;
} else {
result.append(c);
}
}

System.out.println("Alternating Caps: " + result.toString());


}
}

#TASK9
import java.util.Scanner;
public class StrongPasswordChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a password:");
String password = scanner.nextLine();

boolean hasUpperCase = false;


boolean hasLowerCase = false;
boolean hasDigit = false;
boolean hasSpecialChar = false;

for (char c : password.toCharArray()) {


if (Character.isUpperCase(c)) {
hasUpperCase = true;
} else if (Character.isLowerCase(c)) {
hasLowerCase = true;
} else if (Character.isDigit(c)) {
hasDigit = true;
} else if (!Character.isLetterOrDigit(c)) {
hasSpecialChar = true;
}
}

if (password.length() >= 8 && hasUpperCase && hasLowerCase && hasDigit &&


hasSpecialChar) {
System.out.println("True");
} else {
System.out.println("False");
}
}
}

You might also like