> 4. Print the strings that are of even length from an array of strings
Arrays in Java: (Part 5)
One Dimensional Array :
4. Print the strings that are of even length from an array of strings
5. Print the strings that start with a vowel from an array of strings
6. Print the strings that ends with a vowel from an array of strings
- Example: (Print the strings that are of even length)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// find the longest string in a string array import java.util.Scanner; class test { public static void main(String ar[]) { Scanner scan = new Scanner(System.in); System.out.println("Enter the size of array:"); int size = scan.nextInt(); String array[] = new String[size]; scan.nextLine(); // clear the buffer after integer input for(int i=0;i<array.length;i++) { System.out.println("Enter "+i+" string"); array[i] = scan.nextLine(); } for(int i=0;i<array.length;i++) { if(array[i].length()%2==0) // if the length of string is even { System.out.println(array[i]+" has length "+array[i].length()); } } } } /* OUTPUT: Enter the size of array: 5 Enter 0 string blah blah blah Enter 1 string blah blah Enter 2 string blah blah blah blah Enter 3 string blah blah blah blah hahahaha Enter 4 string blah blah blah blah has length 14 blah blah blah blah hahahaha has length 28 blah has length 4 */ - Example: (Print the strings that start with a vowel)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// find the strings that begins with a vowel from an array of Strings. import java.util.Scanner; class test { public static void main(String ar[]) { Scanner scan = new Scanner(System.in); System.out.println("Enter the size of array:"); int size = scan.nextInt(); String array[] = new String[size]; scan.nextLine(); // clear the buffer after integer input for(int i=0;i<array.length;i++) { System.out.println("Enter "+i+" string"); array[i] = scan.nextLine(); } for(int i=0;i<array.length;i++) { if(array[i].charAt(0)=='a' || array[i].charAt(0)=='e' || array[i].charAt(0)=='i' || array[i].charAt(0)=='o' || array[i].charAt(0)=='u')// if the first character is a vowel { System.out.println(array[i]+" begins with a vowel"); } } } } /* OUTPUT: Enter the size of array: 5 Enter 0 string orange Enter 1 string apple Enter 2 string mango Enter 3 string pear Enter 4 string avocado orange begins with a vowel apple begins with a vowel avocado begins with a vowel */ - Example: (Print the strings that end with a vowel)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// find the strings that ends with a vowel import java.util.Scanner; class test { public static void main(String ar[]) { Scanner scan = new Scanner(System.in); System.out.println("Enter the size of array:"); int size = scan.nextInt(); String array[] = new String[size]; scan.nextLine(); // clear the buffer after integer input for(int i=0;i<array.length;i++) { System.out.println("Enter "+i+" string"); array[i] = scan.nextLine(); } for(int i=0;i<array.length;i++) { // charAt(length_of_the_string -1) will give you the last character of that string int x = array[i].length()-1; if(array[i].charAt(x)=='a' || array[i].charAt(x)=='e' || array[i].charAt(x)=='i' || array[i].charAt(x)=='o' || array[i].charAt(x)=='u')// if the first character is a vowel { System.out.println(array[i]+" ends with a vowel"); } } } } /* OUTPUT: Enter the size of array: 5 Enter 0 string orange Enter 1 string mango Enter 2 string banana Enter 3 string pear Enter 4 string kiwi orange ends with a vowel mango ends with a vowel banana ends with a vowel kiwi ends with a vowel */
Comments
Post a Comment