0% found this document useful (0 votes)
43 views5 pages

Java String Manipulation Tasks

Uploaded by

Abdullah Abeed
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)
43 views5 pages

Java String Manipulation Tasks

Uploaded by

Abdullah Abeed
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

//Task1

import [Link];
public class Task1 {
public static void main(String[] args) {
Scanner abeed= new Scanner([Link]);
[Link]("Please enter your input");
String first = [Link]();
int count = [Link]();

for(int i = 0;i<count;i++)
{
char ch = [Link](i);
int asc = (int)ch;

if(asc>=97 && asc<=122)


{
asc-=32;
char cap = (char)asc;
[Link](cap);
}
else
{
[Link](ch);
}
}
}
}

//Task2

import [Link];
public class Task2 {
public static void main(String[] args) {
Scanner abeed= new Scanner([Link]);
[Link]("Please enter your input");
String first = [Link]();
int count = [Link]();
String rev="";

for(int i = count-1; i>=0 ;i--)


{
char ch = [Link](i);
rev+=ch;
}
[Link](rev);
if([Link](first) )
{
[Link]("true");
}
else
{
[Link]("false");
}
}
}
//Task4
import [Link];
public class Task4 {
public static void main(String[] args) {
Scanner abeed= new Scanner([Link]);
[Link]("Please enter your input");
String first = [Link]();
int count = [Link]();
String rev = "";
for(int i = count-1; i>=0 ;i--)
{
char ch = [Link](i);
rev+=ch;
}
[Link](rev);
}
}

//Task5
import [Link];
public class Task5{
public static void main(String[] args){
Scanner abeed = new Scanner([Link]);
[Link]("Enter the input:");
String str = [Link]();

int vow =0;


int con =0;

for(int i = 0; i< [Link]();i++)


{
char ch= [Link](i);

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


{
vow++;
}

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


{
con++;
}
}

if(vow> 0 && con> 0 && vow %3 == 0 && con% 5 == 0)


{
[Link]("Aaarr! Me Plunder!!");
}
else
{
[Link]("Blimey! No Plunder!!");
}
}
}

//Task7
import [Link];
public class Task7 {
public static void main(String[] args) {
Scanner abeed= new Scanner([Link]);
[Link]("Please enter your input");
String first = [Link]();
[Link]("Please enter your input");
String second = [Link]();
int count1 = [Link]();
int count2 = [Link]();

String result ="";


boolean bool = false;
for(int i = 0; i<count1 ;i++)
{
char ch1 = [Link](i);

for(int j = 0; count2>j ;j++)


{
char ch2 = [Link](j);
if(ch1==ch2)
{
bool = true;

}
if(bool== false)
{
result +=ch1;

}
bool= false;

for(int i = 0; i<count2 ;i++)


{
char ch1 = [Link](i);

for(int j = 0; count1>j ;j++)


{
char ch2 = [Link](j);
if(ch1==ch2)
{
bool = true;

}
if(bool== false)
{
result +=ch1;
}
bool= false;

}
[Link](result);
}
}

//Task9
import [Link];
public class Task9 {
public static void main(String[] args) {
Scanner abeed = new Scanner([Link]);

[Link]("Enter the password:");


String password = [Link]();

boolean hasUppercase= false;


boolean hasLowercase= false;
boolean hasDigit= false;
boolean hasSpecialChar= false;

if([Link]()>=8)
{
for(int i = 0; i < [Link](); i++)
{
char ch = [Link](i);

if(ch >='A'&& ch<='Z')


{
hasUppercase= true;
}

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


{
hasLowercase= true;
}

else if(ch >='0' && ch<= '9')


{
hasDigit= true;
}

else if(!(ch >='A' && ch <='Z') && !(ch >='a' && ch<= 'z') && !(ch
>= '0' && ch <='9'))
{
hasSpecialChar= true;
}
}
}

boolean isStrongPassword = hasUppercase && hasLowercase && hasDigit &&


hasSpecialChar && [Link]() >= 8;

if(isStrongPassword)
{
[Link]("True");
}
else
{
[Link]("False");
}

}
}

Common questions

Powered by AI

Task7 identifies unique characters by checking each character from the first input string against all characters of the second string and vice versa. It constructs a result string containing characters that do not appear in the other string. The process utilizes nested loops to compare each character in one string with all characters in the other .

Task2 determines if an input string is a palindrome by first reversing the string and storing it in a variable 'rev'. It then compares 'rev' with the original string to see if they are identical, printing 'true' if they are and 'false' otherwise. This comparison checks if the string reads the same forwards and backwards .

The method used in Task2 and Task4 reverses strings by concatenating each character in reverse order. This approach is inefficient due to the use of string concatenation inside loops, which can lead to O(n^2) complexity due to repeated creation of new strings. Using a StringBuilder, which allows modifications in place, could optimize this to O(n), improving performance significantly .

In Task1, ASCII value manipulation is central for converting lower-case to upper-case letters by adjusting each character's value by 32. Changes in encoding, such as using UTF-16, could affect this logic as ASCII values might not apply, breaking the conversion logic since Unicode may alter character value mapping .

Task9 evaluates password strength based on five criteria: it must be at least 8 characters long and contain at least one upper-case letter, one lower-case letter, one digit, and one special character. The program iterates over the password, using conditional checks to set boolean flags for each criterion. A password is considered strong if all flags are true, indicated by a final conditional check .

Using Scanner for input reading allows for simple and effective user interaction. However, potential pitfalls include resource leaks if Scanner is not closed, and input mismatch exceptions if user inputs do not match expected formats. Also, Scanner's nextLine method reads input as strings only and might capture unexpected new line characters or be overridden by previous next or nextInt calls .

Task5 prints "Aaarr! Me Plunder!!" if the input string contains vowels and consonants such that the number of vowels is divisible by 3 and the number of consonants is divisible by 5. It counts vowels and consonants by iterating over each character and checking if it falls within the respective categories .

Task1 converts lower-case letters to upper-case by iterating through each character of the input string, checking if its ASCII value is between 97 and 122 (the range for lower-case letters). If so, it subtracts 32 from the ASCII value to convert it to its corresponding upper-case character. This subtraction works because in the ASCII table, the difference between upper and lower-case letter values is consistently 32 .

Similar to Task2, Task4 reverses the input string by iterating from the last character to the first and appending each character to a new string 'rev'. However, it differs because Task4 does not compare the reversed string to the original; it simply outputs the reversed string without additional checks for palindrome properties .

Nested loops in Task7 allow every character of the first string to be compared against every character of the second string. The outer loop iterates over characters of one string, while the inner loop enables checking each against characters in the second. This exhaustive traversal ensures all unique characters can be identified by the result of the absence within each comparison .

You might also like