{"id":1372,"date":"2016-09-20T14:19:24","date_gmt":"2016-09-20T14:19:24","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1372"},"modified":"2024-12-14T03:53:04","modified_gmt":"2024-12-14T03:53:04","slug":"write-a-java-program-to-find-vowels-in-a-string","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-java-program-to-find-vowels-in-a-string\/","title":{"rendered":"Write a java program to find vowels in a string?"},"content":{"rendered":"<h2>Write a java program to find vowels in a string?<\/h2>\n<p>Write a java program to find vowels in a string. Write some testcases to test your program output. Sample output and TestCase:<\/p>\n<h4><strong>Sample input to the program<\/strong><\/h4>\n<p>String s = TestingDocs<\/p>\n<h4><strong>Sample output of the program:<\/strong><\/h4>\n<p>Input String: TestingDocs<br \/>\nFirst vowel: e at position 2.<br \/>\nLast vowel: o at position 9.<span id=\"more-1553\"><\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><strong>Java Program<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.javaautomation.questions;\r\n\r\nimport java.util.Scanner;\r\n\r\npublic class VowelStringLocator {\r\n\r\n\/* This method returns true if c is an upper case or lower case vowel\r\n * or false otherwise. Vowels in English alphabets aeiouAEIOU*\/\r\n public static boolean isVowel(char c) {\r\n \/* Checking for both upper case and lower case vowels *\/\r\n \/* || logical or *\/\r\n if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U') \r\n return true;\r\n else\r\n return false;\r\n }\r\n\r\n\/* This method returns the position of the first vowel in s or -1\r\n * if s contains no vowels at all. *\/\r\n public static int positionOfFirstVowel(String s) {\r\n char c;\r\n \/* in a loop check for each character in s for a vowel *\/\r\n \/* if vowel return the index and stop *\/\r\n for (int i = 0; i &lt; s.length(); i++) {\r\n c = s.charAt(i);\r\n if (isVowel(c)) \r\n return i + 1;\r\n }\r\n return -1;\r\n }\r\n\r\n\/* returns the position of the last occurrence of a vowel in\r\n * s or -1 if s contains no vowels at all. It is just the same as above\r\n * method just that we check the string from reverse *\/\r\n public static int positionOfLastVowel(String s) {\r\n char c;\r\n \/* in a loop check for each character in s for vowel *\/\r\n \/* if vowel return the index and stop *\/\r\n for (int i = s.length() -1 ; i &gt;= 0; i--) {\r\n c = s.charAt(i);\r\n if (isVowel(c)) \r\n return i + 1;\r\n }\r\n return -1;\r\n }\r\n\r\n\r\n\r\npublic static void main(String[] args) {\r\n \/\/ TODO Auto-generated method stub\r\n Scanner input = new Scanner(System.in);\r\n String s;\r\n System.out.println(\"Please enter Input String\");\r\n s = input.next();\r\n System.out.println(\"Input String is:\" + s);\r\n int position = positionOfFirstVowel(s);\r\n if(position != -1)\r\n {\r\n System.out.println(\"First vowel:\" + s.charAt(position-1) + \" at position \" + position);\r\n }\r\n else {\r\n System.out.println(\"First vowel:NOT FOUND\");\r\n }\r\n \r\n position = positionOfLastVowel(s);\r\n if(position != -1)\r\n {\r\n System.out.println(\"Last vowel:\" + s.charAt(position-1) + \" at position \" + position);\r\n }\r\n else {\r\n System.out.println(\"Last vowel:NOT FOUND\");\r\n }\r\n input.close();\r\n\r\n}\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>Output of the program.<\/strong><\/h3>\n<p>Please enter Input String<br \/>\nTestingDocs<br \/>\nInput String is:TestingDocs<br \/>\nFirst vowel:e at position 2<br \/>\nLast vowel:o at position 9<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1554\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Vowels-in-String-Locator-.png\" alt=\"Vowels in String Locator\" width=\"1114\" height=\"679\" title=\"\"><\/p>\n<p>Test case<\/p>\n<p>Please enter Input String<br \/>\n123456789<br \/>\nInput String is:123456789<br \/>\nFirst vowel:NOT FOUND<br \/>\nLast vowel:NOT FOUND<\/p>\n<p>Test case<\/p>\n<p>Please enter Input String<br \/>\nApple a day keeps doctor away!<br \/>\nInput String is:Apple<br \/>\nFirst vowel:A at position 1<br \/>\nLast vowel:e at position 5<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a java program to find vowels in a string? Write a java program to find vowels in a string. Write some testcases to test your program output. Sample output and TestCase: Sample input to the program String s = TestingDocs Sample output of the program: Input String: TestingDocs First vowel: e at position 2. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1372","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1372"}],"version-history":[{"count":8,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1372\/revisions"}],"predecessor-version":[{"id":26359,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1372\/revisions\/26359"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}