diff --git a/1ano/2semestre/poo/guides/POO-2021-aula03-extra.pdf b/1ano/2semestre/poo/guides/POO-2021-aula03-extra.pdf new file mode 100644 index 0000000..29a22e2 Binary files /dev/null and b/1ano/2semestre/poo/guides/POO-2021-aula03-extra.pdf differ diff --git a/1ano/2semestre/poo/src/aula03/StringExtras.java b/1ano/2semestre/poo/src/aula03/StringExtras.java new file mode 100644 index 0000000..d5db692 --- /dev/null +++ b/1ano/2semestre/poo/src/aula03/StringExtras.java @@ -0,0 +1,24 @@ +package aula03; + +import java.util.Scanner; +import utils.StringMethods; + +public class StringExtras { + public static void main(String[] args) { + Scanner sin = new Scanner(System.in); + + String str = sin.nextLine(); + + System.out.println("Frase convertida para minúsculas: " + str.toLowerCase()); + System.out.println("Último caracter da frase: " + str.substring(str.length()-1)); + System.out.println("Os 3 primeiros caracteres: " + str.substring(0, 3)); + System.out.printf("Número de digitos na frase: %d\n", StringMethods.countDigits(str)); + System.out.printf("Número de espaços na frase: %d\n", StringMethods.countSpaces(str)); + System.out.printf("A frase contém apenas letras minúsculas: %b\n", StringMethods.isAllLower(str)); + System.out.println("Frase sem espaços duplicados: " + StringMethods.removeDuplicatedSpaces(str)); + System.out.println("A string é um palindromo: " + StringMethods.isPalindrome(str)); + System.out.println("Acrónimo da string: " + StringMethods.getAcronym(str)); + + sin.close(); + } +} diff --git a/1ano/2semestre/poo/src/utils/StringMethods.java b/1ano/2semestre/poo/src/utils/StringMethods.java new file mode 100644 index 0000000..bbeba80 --- /dev/null +++ b/1ano/2semestre/poo/src/utils/StringMethods.java @@ -0,0 +1,51 @@ +package utils; + +public class StringMethods { + public static int countDigits(String s) { + int count = 0; + for (char c : s.toCharArray()) + if (Character.isDigit(c)) + count++; + return count; + } + + public static int countSpaces(String s) { + int count = 0; + for (char c : s.toCharArray()) + if (c == ' ') + count++; + return count; + } + + public static boolean isAllLower(String s) { + for (char c : s.toCharArray()) + if (Character.isUpperCase(c)) + return false; + return true; + } + + public static String removeDuplicatedSpaces(String s) { + StringBuilder newStr = new StringBuilder(); + for (char c : s.toCharArray()) + if (c != ' ' || !newStr.substring(newStr.length() - 1).equals(" ")) + newStr.append(c); + return newStr.toString(); + } + + public static boolean isPalindrome(String s) { + char[] chars = s.toCharArray(); + for (int i = 0; i < s.length() / 2; i++) + if (chars[i] != chars[chars.length - 1 - i]) + return false; + return true; + } + + public static String getAcronym(String s) { + String[] words = s.split(" "); + StringBuilder acronym = new StringBuilder(); + for (String word : words) + if (word.length() >= 3) + acronym.append(word.charAt(0)); + return acronym.toString().toUpperCase(); + } +}