Assignment 3
Assignment 3
Section:- CSB-06
Q1)
//Write a method that counts the number of letters in a string using the
following header:
// public static int countLetters(String s)
// Write a test program that prompts the user to enter a string and displays
the number of letters in the
// string.
import java.util.*;
public class Q1 {
public static int countLetters(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (Character.isLetter(s.charAt(i)))
count++;
}
System.out.println("Your String contains:- "+ count + " number of
letters.");
return count;
}
Output:-
Q2)
// Some websites impose certain rules for passwords. Write a method that
checks whether a string is a
// valid password. Suppose the password rules are as follows:
// ■ A password must have at least eight characters.
// ■ A password consists of only letters and digits.
// ■ A password must contain at least two digits.
// Write a program that prompts the user to enter a password and displays
Valid Password if the rules
// are followed or Invalid Password otherwise.
import java.util.*;
public class Q2 {
public static void checkPassword(String s) {
int count = 0;
if(s.length() < 8) {
System.out.println("Password too short.");
} else {
for(int i = 0;i<s.length();i++) {
if (Character.isDigit(s.charAt(i)))
count++;
if(Character.isLetterOrDigit(s.charAt(i)) == false) {
System.out.println("Invalid Password");
}
}
if(count < 2)
System.out.println("Invalid Password");
else
System.out.println("Valid Password");
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Input String:- ");
String s = input.nextLine();
checkPassword(s);
}
}
Output:-
Q3)
//Write a method that counts the number of letters in a string using the
following header:
// public static int countLetters(String s)
// Write a test program that prompts the user to enter a string and displays
the number of letters in the
// string.
import java.util.*;
public class Q1 {
public static int countLetters(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (Character.isLetter(s.charAt(i)))
count++;
}
System.out.println("Your String contains:- "+ count + " number of
letters.");
return count;
}
Output:-
Q4)
//Write a program that prompts the user to enter two strings and displays the
largest common prefix
// of the two strings. Here are some sample runs:
// Enter the first string: Welcome to C++
// Enter the second string: Welcome to programming
// The common prefix is Welcome to
// Enter the first string: Atlanta
// Enter the second string: Macon
// Atlanta and Macon have no common prefix
import java.util.*;
public class Q4 {
public static void commonPrefix(String s1, String s2) {
String small, large;
if (s1.length() > s2.length()) {
small = s2;
large = s1;
} else {
small = s1;
large = s2;
}
int index = 0;
for (char c : large.toCharArray()) {
if (index == small.length())
break;
if (c != small.charAt(index))
break;
index++;
}
if (index == 0)
System.out.println("" + s1 + " and " + s2 + " have no common
prefix");
else
System.out.println(large.substring(0, index));
}
commonPrefix(s1, s2);
}
}
Output:-
Q5)
import java.util.Scanner;
public class Q5 {
public static boolean isSSNValidFormat(String s) {
char ch = s.charAt(i);
if (i == 3 && ch != '-')
return false;
if (i == 6 && ch != '-')
return false;
if (i != 6 && i != 3 && !Character.isDigit(ch))
return false;
}
return true;
}
input.close();
if(isSSNValidFormat(s))
System.out.println(s + " is a valud social securty number.");
else
System.out.println(s + " is a INVALID social security number.");
}
}
Output:-
Q6)
// Write a program that prompts the user to enter two strings and reports
whether the second string is a substring of the first String
import java.util.*;
public class Q6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Input String s1:- ");
String s1 = input.nextLine();
System.out.println("Input String s2:- ");
String s2 = input.nextLine();
if (s1.contains(s2))
System.out.println(s2 + " is a substring of " + s1);
else
System.out.println(s2 + "Not a substring of " + s1);
}
}
Output:-
Q7)
// Write the following method that tests whether the array has four
consecutive numbers with the
// same value.
import java.util.*;
public class Q7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of values:");
if (isConsecutiveFour(values)) {
System.out.println("The list has consecutive fours");
} else {
System.out.println("The list has no consecutive fours");
}
}
count++;
}
if (count == 4)
break;
}
if (count == 4)
return true;
return false;
}
}
Output:-
Q8)
import java.util.Scanner;
public class Q8 {
printArray(eliminateDuplicates(numbers), 10);
}
}
}
if (!isDuplicate) {
temp[tempIndex++] = list[i];
}
}
int[] trimmedArray = new int[tempIndex];
for (int i = 0; i < tempIndex; i++) {
trimmedArray[i] = temp[i];
}
return trimmedArray;
}
Output:-
Q9)
// The arrays list1 and list2 are identical if they have the same contents.
Write a method that
// returns true if list1 and list2 are identical, using the following header:
import java.util.*;
public class Q9 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
sort(list1);
sort(list2);
for (int i = 0; i < list1.length; i++) {
if (list1[i] != list2[i])
return false;
}
return true;
}
if (minIndex != i) {
list[minIndex] = list[i];
list[i] = min;
}
}
}
}
Output:-