How to find vowel "i" in the string which is inputed by user ? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

How to find vowel "i" in the string which is inputed by user ?

9th Sep 2016, 10:34 PM
nisa905
nisa905 - avatar
7 Réponses
+ 2
http://code.sololearn.com/cq0a4WEE0KuA import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Please enter a word: "); String s = sc.nextLine(); System.out.println(s); int i; for(i = 0; i < s.length(); i++) { if (s.charAt(i) == 'i') { break; } } if (i != s.length()) { System.out.println("i found at index " + i); } else { System.out.println("i not found"); } } }
9th Sep 2016, 11:14 PM
Zen
Zen - avatar
+ 2
You could use indexOf to get the sum od i's see below http://code.sololearn.com/ceIHXOdCdlhx/#java public class Main { public static void main(String[] args) { String word = "His ice-cream in his pocket"; int i = -1; int sum = 0; System.out.println(); while ((i=word.indexOf("i",i+1)) != -1) { ++sum; } System.out.println("number of i in '" + word+ "' is " + sum); } }
11th Sep 2016, 4:05 PM
Tiger
Tiger - avatar
+ 1
break is to break out of the loop.
9th Sep 2016, 11:27 PM
Zen
Zen - avatar
0
what function of "break"?
9th Sep 2016, 11:15 PM
nisa905
nisa905 - avatar
0
This method has following different variants: public int indexOf(int ch): Returns the index within this string of the first occurrence of the specified character or -1 if the character does not occur. public int indexOf(int ch, int fromIndex): Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index or -1 if the character does not occur. int indexOf(String str): Returns the index within this string of the first occurrence of the specified substring. If it does not occur as a substring, -1 is returned. int indexOf(String str, int fromIndex): Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. If it does not occur, -1 is returned. Syntax: Here is the syntax of this method: public int indexOf(int ch ) or public int indexOf(int ch, int fromIndex) or int indexOf(String str) or int indexOf(String str, int fromIndex) Parameters: Here is the detail of parameters: ch -- a character. fromIndex -- the index to start the search from. str -- a string. source http://www.tutorialspoint.com/java/java_string_indexof.htm
10th Sep 2016, 8:30 AM
Tiger
Tiger - avatar
0
so how i sum of 'i' vowel in that string?
11th Sep 2016, 12:02 PM
nisa905
nisa905 - avatar
0
if any body know,how to find vowel in own name ,for ex,-which letter is vowel, Name-avinash raj here a,i is vowel
6th Aug 2017, 6:54 PM
AVINASH RAJ
AVINASH RAJ - avatar