how to find index of first vowel in a string?
First of all, your break
should break out of both loops. To do so, put a flag before your first loop like
outerloop:
for (int i = 0; i < word.length(); i++) {
Then instead of break;
put break outerloop;
Second,
word = word.substring(i + 1) + word.substring(i) + "ay";
doesn't work, because when you find the vowel, you want to keep it in front, so i + 1
should be just i
. Then, the rest of the substring before "ay" should be the consonants at the beginning, so 0, i
instead of just i
. In all, this gives
outerloop:
for (int i = 0; i < word.length(); i++) {
for (int j = 0; j < 5; j++) {
if (word.charAt(i) == vowel[j]) {
word = word.substring(i) + word.substring(0, i) + "ay";
break outerloop;
}
}
}
Finding where the first vowel in a word occurs
Here's one way you could do it:
final static String vowels = "aeiouy";
public static int indexOfFirstVowel(String word){
String loweredWord = word.toLowerCase();
for (int index = 0; index < loweredWord.length(); index++)
{
if (vowels.contains(String.valueOf(loweredWord.charAt(index))))
{
return index;
}
}
// handle cases where a vowel is not found
return -1;
}
This simply walks through the word by character, and checks each character to see if it exists within your vowels String.
Extract the index of the first vowel in a string
Here is a solution using a simple For loop which you might be familiar with and enumerate:
def position_of_first_vowel(word):
for index, char in enumerate(word):
if char in 'aeiou':
return index
return -1
for case in ["python","stock", "overflow", "styling", "zxc"]:
print("%s: %d" % (case, position_of_first_vowel(case)))
Output:
python: 4
stock: 2
overflow: 0
styling: 4
zxc: -1
N.B. The extra test case of zxc
is there to show the returning of -1
if a vowel does not occur in the word
Find first vowel from string and add it to a var
You do not need to add the date part by part to avoid -
, it is better to split it by -
and join the parts together to get desired result. And you can use a loop to find first vowel:
private String getRFC(){
String vowels = "AEIOUaeiou";
String apellidoPat = "CRUZ";
String apellidoMat = "HERNANDEZ";
String fechaNac = "1997-12-25";
// first letter of apellidoPat
String rfc = String.valueOf(apellidoPat.charAt(0));
// first vowel of apellidoPat
for (int i = 0; i < apellidoPat.length(); ++i){
if (vowels.indexOf(apellidoPat.charAt(i)) != -1){
rfc += String.valueOf(apellidoPat.charAt(i));
break;
}
}
// first letter of apellidoMat + date (YYMMDD)
rfc += apellidoMat.charAt(0) + String.join("", fechaNac.split("-"));
return rfc;
}
Finding first vowel of a word in Python
Your code always return len(word)-1
if the character in position 1
is not a vowel. Also elephant
didn't work and spa
works only because of the bug I mentioned, it returns 2
which is len(word)-1
and not the index of the vowel found. Try debug your code line by line and you'll figure it out very quickly.
This could be a working code that return -1
if there's no vowel, else return the index of the first vowel found.
def find_first_vowel(word):
i = 0
while i < len(word):
if word[i] in vowels:
return i
i += 1
return -1
EDIT
If you want to return the last character if there's no vowels just change return -1
with return len(word) - 1
. Here:
def find_first_vowel(word):
i = 0
while i < len(word):
if word[i] in vowels:
return i
i += 1
return len(word) - 1
Finding the first index of a vowel in a given string, otherwise return -1 if there aren't any
You're getting the int repesentation of the char, you actually want to return i as this shows the position where you found the char.
I need to find the position of the first vowel in a string in php
Use the strcspn
function. It will return the length of the string at the beginning not matching any of the characters you specified.
$pos = strcspn(strtolower($str), "aeiou"); // and sometimes y
Related Topics
Maven Builds Are Working, But Junit Is Throwing a Nosuchmethoderror
Error Starting Applicationcontext in Spring Boot App
Get Requestbody and Responsebody At Handlerinterceptor
Netbeans Tomcat Build Failed - the Module Has Not Been Deployed
In Firebase After Uploading Image How to Get Url
How to Get the Autoincremented Id When I Insert a Record in a Table Via Jdbctemplate
How to Convert Date Which I Got from Firebase Server as Am Getting Error Date
Crudrepository How to Find by Last Date Between
Iterating Over Two Arrays Simultaneously Using for Each Loop in Java
String.Replaceall Single Backslashes With Double Backslashes
Loop Over All Fields in a Java Class
How to Test for Blank Line With Java Scanner
Is There an Invisible Character That Is Not Regarded as Whitespace
How to Pass a List and a Separate String Using @Requestbody in Springboot
How to Post Form Data With Spring Resttemplate