├── Replace_space_with_hypens.java ├── Captalise each word.java ├── Remove_specific_char.java ├── Reverse_each_words.java ├── Palindrome.java ├── Longest word.java ├── Count_of_Substring.java ├── Remove_duplicates.java ├── vowel_and_consonent_Conunt.java └── README.md /Replace_space_with_hypens.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | /* 4 | Write a program that replaces all spaces in a string with hypens. 5 | Input: "Hello World Welcome you" 6 | Output: "Hello-World-Welcome-you" 7 | */ 8 | 9 | public class Replace_space_with_hypens { 10 | public static void main(String[] args) { 11 | System.out.print((new Scanner(System.in)).nextLine().replace(" ", "-")); // One line code 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Captalise each word.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | /* 4 | Write a program that capitalizes the first letter of each word in a given string. 5 | 6 | Input: "hello Good morning everybody" 7 | Output: "Hello Good Morning Everybody" 8 | */ 9 | 10 | class Captalise_each_word { 11 | public static void main(String[] args) { 12 | Scanner sc = new Scanner(System.in); 13 | String[] strval = sc.nextLine().split(" "); 14 | for (String word : strval) { 15 | System.out.print(word.substring(0, 1).toUpperCase()); 16 | System.out.print(word.substring(1) + " "); 17 | } 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Remove_specific_char.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | /* Write a program that removes all occurrences of a specific character from a String. 4 | Input: "Hello World" and 'o' 5 | Output: "Hell Wrld" 6 | 7 | */ 8 | 9 | public class Remove_specific_char { 10 | public static void main(String arun[]) { 11 | Scanner sc = new Scanner(System.in); 12 | String strval = sc.nextLine(); 13 | char ch = sc.next().charAt(0); 14 | for (int index = 0; index < strval.length(); index++) { 15 | if (ch != strval.charAt(index)) { 16 | System.out.print(strval.charAt(index)); 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Reverse_each_words.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | /* 4 | Write a program that reverses each word in a given string. 5 | Input: "Hello World see me" 6 | Output: "olleH dlroW ees em" 7 | */ 8 | 9 | public class Reverse_each_words { 10 | public static void main(String[] args) { 11 | Scanner sc = new Scanner(System.in); 12 | String[] strval = sc.nextLine().split(" "); 13 | for (String word : strval) { 14 | String temp = ""; 15 | for (int index = word.length() - 1; index >= 0; index--) { 16 | temp += word.charAt(index); 17 | } 18 | System.out.print(temp + ' '); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Palindrome.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /* 4 | Write a program that checks if a given string is a palindrome. 5 | 6 | Input: "madam" 7 | Output: "True" 8 | */ 9 | 10 | public class Palindrome { 11 | public static void main(String[] args) { 12 | Scanner sc = new Scanner(System.in); 13 | String strval = sc.next(); 14 | String revstr = ""; 15 | for (int i = strval.length() - 1; i >= 0; i--) { 16 | revstr = revstr + strval.charAt(i); 17 | } 18 | System.out.print(revstr); 19 | if (revstr.intern() == strval.intern()) { 20 | System.out.print("True"); 21 | } else { 22 | System.out.print("False"); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Longest word.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | /* 3 | Write a program that finds the longest word in the given string. 4 | Input: 5 | "The quick brown for jumps over the lazy dog" 6 | Output: 7 | Longest word: jumps 8 | */ 9 | 10 | class Longest_word { 11 | public static void main(String[] args) { 12 | Scanner sc = new Scanner(System.in); 13 | int len = 0; 14 | String longword = ""; 15 | String[] strval = sc.nextLine().split(" "); 16 | for (String word : strval) { 17 | if (word.length() >= len) { 18 | len = word.length(); 19 | longword = word; 20 | } 21 | } 22 | System.out.println("Longest word:" + longword); 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Count_of_Substring.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /* 4 | Write a program that counts the number of times a given substring appears in a string. 5 | Input: "banana" and "an" 6 | Output: "The substring count is 2" 7 | */ 8 | 9 | public class Count_of_Substring { 10 | public static void main(String r[]) { 11 | Scanner sc = new Scanner(System.in); 12 | String strval = sc.next(); 13 | String substr = sc.next(); 14 | int count = 0; 15 | for (int iter = 0; iter < strval.length() - substr.length() + 1; iter++) { 16 | if (strval.substring(iter, iter + substr.length()).intern() == substr.intern()) { 17 | count++; 18 | } 19 | } 20 | System.out.print("The substring count is " + count); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Remove_duplicates.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /* 4 | Write a program that removes duplicate characters from string. 5 | 6 | Input: "hello" 7 | Output: "helo" 8 | */ 9 | 10 | public class Remove_duplicates { 11 | public static void main(String[] args) { 12 | Scanner sc = new Scanner(System.in); 13 | String strval = sc.next(); 14 | int count = 0; 15 | for (int i = 0; i < strval.length(); i++) { 16 | char temp = strval.charAt(i); 17 | for (int j = 0; j < i + 1; j++) { 18 | if (strval.charAt(j) == temp) { 19 | count++; 20 | } 21 | } 22 | if (count == 1) { 23 | System.out.print(temp); 24 | } 25 | count = 0; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /vowel_and_consonent_Conunt.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /* 4 | Write a program that counts the number of vowels and consonants in a given string. 5 | Input: "Hello World" 6 | Output: 7 | Vowels Count: 3 8 | Consonants Count: 7 9 | */ 10 | 11 | public class vowel_and_consonent_Conunt { 12 | public static void main(String[] args) { 13 | Scanner sc = new Scanner(System.in); 14 | int vowelCount = 0, consonantsCount = 0; 15 | String[] strval = sc.nextLine().split(" "); 16 | for (String word : strval) { 17 | for (int iter = 0; iter < word.length(); iter++) { 18 | char ch = word.charAt(iter); 19 | if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' 20 | || ch == 'u' || ch == 'U') { 21 | vowelCount++; 22 | } else { 23 | if (ch != '!' && ch != ',' && ch != '.' && ch != '?') { 24 | consonantsCount++; 25 | } 26 | } 27 | } 28 | } 29 | System.out.println("Vowels Count:" + vowelCount); 30 | System.out.println("Consonants Count:" + consonantsCount); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🎉 Welcome to my Java_Tasks_BasedOn_String_Class Repository! 🎉 2 | 3 | ## 👋 Overview 4 | #### This repository showcases my skills in manipulating strings using various methods from the Java String class. It consists of 9 different Java code files, each demonstrating a unique string manipulation technique. From capitalizing words to counting vowels and consonants, this repository covers a range of string operations that are essential for any Java developer. 5 | 6 | ## 📂 File List 7 | 1. `Capitalise Each Word.java 📈` 8 | Uppercases the first letter of every word in a given string using split(), toUpperCase(), and substring().
9 | Example: "hello world" becomes "Hello World" 10 | 2. `Count of Substring.java 🔍` 11 | Counts the occurrences of a substring within a given string using length(), substring(), and intern().
12 | Example: "hello world" contains 1 occurrence of "world" 13 | 3. `Longest Word.java 📊` 14 | Finds the longest word in a list of strings using length() and split().
15 | Example: ["hello", "world", "java"] returns "world" 16 | 4. `Palindrome.java 🔁` 17 | Checks if a given string is a palindrome using charAt(), length(), and intern().
18 | Example: "madam" is a palindrome, while "hello" is not 19 | 5. `Remove Duplicates.java 🚫` 20 | Removes duplicate characters from a given string using length() and charAt().
21 | Example: "hello world" becomes "helo wrd" 22 | 6. `Remove Specific Char.java 🚫` 23 | Removes a specified character from a given string using charAt() and length().
24 | Example: "hello world" with 'h' removed becomes "ello world" 25 | 7. `Replace Space with Hyphens.java 📋` 26 | Replaces all spaces in a given string with hyphens using replace().
27 | Example: "hello world" becomes "hello-world" 28 | 8. `Reverse Each Word.java 🔙` 29 | Reverses each word in a given sentence using split(), length(), and charAt().
30 | Example: "hello world" becomes "olleh dlrow" 31 | 9. `Vowel and Consonant Count.java 📊` 32 | Counts the number of vowels and consonants in a given string using split(), length(), and charAt().
33 | Example: "hello world" contains 3 vowels and 7 consonants 34 | 35 | ## 💻 Purpose 36 | #### This repository demonstrates my understanding of various string manipulation techniques using the Java String class. I hope you find it helpful in your own coding journey! 🤖 37 | 38 | ## 👍 Feedback 39 | #### Feel free to leave feedback or suggestions in the issues section. Let's improve this repository together! 🤝 40 | 41 | ## 👏 Acknowledgement 42 | #### Thanks for visiting my repository! I hope you found it useful. Happy coding! 💻 43 | 44 | ## 👍 Support 45 | #### If you'd like to support my coding journey, consider starring this repository or sharing it with your network. Let's grow the coding community together! 🤜🤛 46 | --------------------------------------------------------------------------------