Find Position of Left most Vowel: JavaScript
Develop and demonstrate a xhtml file which uses javascript program to find the first occurrence or the left most vowel in the user entered string.
Input: A String.
Output: Position of the left most or first occurrence of the vowel in the user entered string.
Video Explaining The JavaScript Code:
Work Flow:
Step 1: Prompt the user to enter a string, store the user entered string in a variable called str.
Step 2: Convert the string in str to upper case using toUpperCase function. Now the original value of str is lost and its equivalent upper case string has been stored.
Ex: str = str.toUpperCase();
Step 3: Now find the length of the entered string using length function. i.e., variableName.length; Ex: str.length;
Step 4: Using for loop, loop through the string from 0 to the string length(which is calculated in Step 3).
Step 5: Using chatAt function fetch one character at a time from str and store it in variable chr. Ex: chr = str.charAt(i);
Step 6: Now compare the value in chr with upper case vowels: A, E, I, O, U.
Step 7: If chr matches any vowel then break out of for loop.
Step 8: If the value of i after coming out of the for loop, is less than string length then print the value of i which is the position of the left most occurrence of the vowel. If the value of i is greater than or equal to string length, then Vowel not found in the entered string.
Javascrip coding explained in above video:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<html>
<head><title>Finding Left most Vowel in the Input String</title></head>
<body>
<script type="text/javascript">
<!--
var chr = "";
var str = prompt("Enter the string","");
str = str.toUpperCase();
for(var i = 0; i < str.length; i++)
{
chr = str.charAt(i);
if(chr == 'A' || chr == 'E' || chr == 'I'
|| chr == 'O' || chr == 'U')
break;
}
if( i < str.length )
document.write("The position of the
left most vowel is "+(i+1)+"<br />");
else
document.write("No vowel found in the entered string");
// -->
</script>
</body>
</html> |
Input/Output:
If Microsoft is the input string, then the position of the first occurrence of vowel is 2. i.e., i.
For Oracle, O is a vowel, so the position is 1.
For Technotip, e is the first vowel, so the position is 2.
For any input which do not have a vowel in it, the above javascript program outputs this statement: No vowel found in the entered string
No related posts.
Get FREE blog updates to your email inbox. Enter your email ID and subscribe:
Start Making Money From Your Programming Skills
- Subscribe to Technotip.com blog update and you will be able to download "Tips, Tricks and Strategies to Make Money Online" eBook for free.
- You will also receive tips to improve your programming skills, and strategies to make money from your programming skills. We will also send useful resources for learning and building your application.
- You can also make money with us, by just recommending our website to your friends and family. You will get complete strategy for making $300 and more in the ebook that we will send you - once you subscribe to our free blog update using the below form..


Leave a Reply