LAB 7 HOMEWORK

mail@pastecode.io avatar
unknown
java
18 days ago
10 kB
92
Indexable
Never
//Homework One

import java.util.Scanner;
public class HomeworkOne {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String string_one = sc.nextLine();
        String str_1 = "";
        String str_2 = "";


        for (int i = 0; i < string_one.length(); i++) {
            char c = string_one.charAt(i);
            if (c == ' ') {
                str_2 = str_1 + " " + str_2;
                str_1 = "";
            }
            else {
                str_1 += c;
                if ( i == string_one.length()-1) {
                    str_2 = str_1 + " " + str_2;

                }
            }
        }
        System.out.println(str_2);
    }
}

//Homework Two

import java.util.Scanner;

public class HomeworkTwo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str_one = sc.nextLine();
        String str_two = "";

        char current_char = str_one.charAt(0);
        int count = 1;

        for (int i = 1; i < str_one.length(); i++) {
            char b = str_one.charAt(i);
            if (b == current_char) {
                count++;
            }
            else {
                str_two += current_char + "" + count;
                current_char = b;
                count = 1;
            }
        }

        str_two += current_char + "" + count;
        System.out.println(str_two);
    }

}

// Homework Three

import java.util.Scanner;
public class HomeworkThree {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String string_one = sc.nextLine();
        String string_two = sc.nextLine();
        String unique_one = "";
        String unique_two = "";
        String final_string = "";
        String final_string_capitalized = "";

        
        for (int i = 0; i < string_one.length(); i++) {
            char ch_one = string_one.charAt(i);
            boolean match_found = false;
            for (int j = 0; j < string_two.length(); j++) {
                char ch_two = string_two.charAt(j);
                if (ch_one == ch_two) {
                    match_found = true;
                    break;
                }
            }
            if (match_found != true) {
                unique_one += ch_one;
            }
        }

        
        for (int i = 0; i < string_two.length(); i++) {
            char ch_one = string_two.charAt(i);
            boolean match_found = false;
            for (int j = 0; j < string_one.length(); j++) {
                char ch_two = string_one.charAt(j);
                if (ch_one == ch_two) {
                    match_found = true;
                    break;
                }
            }
            if (match_found != true) {
                unique_two += ch_one;
            }
        }

        
        final_string = unique_one + unique_two;

        
        for (int i = 0; i < final_string.length() ; i++) {
            int ascii = final_string.codePointAt(i);
            if (ascii >= 97 && ascii <= 122) {
                int ascii_two = ascii - 32;
                char char_1 = (char) ascii_two;
                final_string_capitalized += char_1;
            }
            else {
                char char_2 = (char) ascii;
                final_string_capitalized += char_2;

            }

        }
        System.out.println(final_string_capitalized);




    }
}

//Homework Four

import java.util.Scanner;

public class HomeworkFour {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String string_one = sc.nextLine();
        int count = 0;

        for (int i = 0; i < string_one.length(); i++) {
            char a = string_one.charAt(i);
            int ascii_fl = (int) a;
            if (i == 0) {
                if (ascii_fl >= 65 && ascii_fl <= 90) {
                    int ascii_fl_new = ascii_fl + 32;
                    char b = (char) ascii_fl_new;
                    count++;
                    System.out.print(b);
                }
                else {
                    char c = (char) ascii_fl;
                    count++;
                    System.out.print(c);
                }

            }
            else if (count > 0 && count % 2 != 0) {
                if (ascii_fl >= 97 && ascii_fl <= 122) {
                    int ascii_fl_new = ascii_fl - 32;
                    char b = (char) ascii_fl_new;
                    count++;
                    System.out.print(b);
                }
                else if (ascii_fl >= 32 && ascii_fl <= 47) {
                    char c = (char) ascii_fl;
                    System.out.print(c);
                }
                else {
                    char d = (char) ascii_fl;
                    count++;
                    System.out.print(d);
                }
            }
            else if (count > 0 && count % 2 == 0) {
                if (ascii_fl >= 65 && ascii_fl <= 90) {
                    int ascii_fl_new = ascii_fl + 32;
                    char b = (char) ascii_fl_new;
                    count++;
                    System.out.print(b);
                }
                else if (ascii_fl >= 32 && ascii_fl <= 47) {
                    char c = (char) ascii_fl;
                    System.out.print(c);
                }
                else {
                    char d = (char) ascii_fl;
                    count++;
                    System.out.print(d);
                }

            }
            else {
                char b = (char) ascii_fl;
                count++;
                System.out.print(b);
            }
        }
    }
}

// Homework Five

import java.util.Scanner;

public class HomeworkFive {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String string_one = sc.nextLine();
        String string_two = sc.nextLine();
        String unique_two = "";
        String final_str = "";
        char ch_x = ' ';
        char ch_y = ' ';

        for (int i = 0; i < string_two.length(); i++) {
            char ch_one = string_two.charAt(i);
            int ch_one_ascii = (int) ch_one;
            if (ch_one_ascii >= 65 && ch_one_ascii <= 90) {
                int ascii_two = ch_one_ascii + 32;
                ch_y = (char) ascii_two;
            }
            else if (ch_one_ascii >= 97 && ch_one_ascii <= 122) {
                int ascii_two = ch_one_ascii - 32;
                ch_y = (char) ascii_two;
            }
            boolean match_found = false;
            for (int j = 0; j < string_one.length(); j++) {
                if (string_one.charAt(j) == ch_one || string_one.charAt(j) == ch_y) {
                    match_found = true;
                    break;
                }
            }
            if (match_found == true) {
                unique_two += ch_one;
            }

        }
        for (int i = 0; i < unique_two.length(); i++) {
            char ch_one = unique_two.charAt(i);
            int ch_one_ascii = (int) ch_one;
            if (ch_one_ascii >= 65 && ch_one_ascii <= 90) {
                int ascii_two = ch_one_ascii + 32;
                ch_x = (char) ascii_two;
            }
            else if (ch_one_ascii >= 97 && ch_one_ascii <= 122) {
                int ascii_two = ch_one_ascii - 32;
                ch_x = (char) ascii_two;
            }

            for (int j = 0; j < string_one.length(); j++) {
                char ch_two = string_one.charAt(j);
                if (ch_one == ch_two || ch_x == ch_two) {
                    final_str += "";
                } else {
                    final_str += ch_two;
                }


            }
            string_one = final_str;
            final_str = "";

        }
        System.out.println(string_one);
    }


}

//Homework Six

import java.util.Scanner;
public class HomeworkSix {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String passkey = sc.nextLine();
        boolean contains_uppercase = false;
        boolean contains_lowercase = false;
        boolean contains_digits = false;
        boolean contains_specialchar = false;
        boolean password_eligibility = false;

        if (passkey.length() >= 8) {
            for (int i = 0; i < passkey.length(); i++) {
                char a = passkey.charAt(i);
                int a_ascii = (int) a;

                if (a_ascii >= 65 && a_ascii <= 90) {
                    contains_uppercase = true;
                }
                else if (a_ascii >= 97 && a_ascii <= 122) {
                    contains_lowercase = true;
                }
                else if (a_ascii >= 48 && a_ascii <= 57) {
                    contains_digits = true;
                }
                else if (a_ascii == 33 && a_ascii <= 47) {
                    contains_specialchar = true;
                }
                else if (a_ascii != 32) {
                    contains_specialchar = true;
                }

            }
        }

        if (contains_digits  && contains_lowercase && contains_uppercase && contains_specialchar && passkey.length() > 8) {
           password_eligibility = true;
           System.out.println(password_eligibility);

        }
        else {
            System.out.print(password_eligibility);
        }


    }
}

//Homework Seven

import java.util.Scanner;
public class HomeworkSeven {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str_one = sc.nextLine();
        String str_two = sc.nextLine();
        int count = 0;

        for (int i = 0; i < 1; i++) {
            char ch_one = str_two.charAt(i);
            for (int j = 0; j < str_one.length(); j++) {
                char ch_three = str_one.charAt(j);
                if (ch_one == ch_three && i != str_one.length()-1) {
                    if (str_two.charAt(i + 1) == str_one.charAt(j + 1))
                    count++;
                }
            }


        }
        System.out.println("Occurence: " + count + " times");



    }
}

Leave a Comment