Write a java program for count a num of variables in a string. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a java program for count a num of variables in a string.

28th Jul 2016, 7:00 AM
honey
honey - avatar
8 Answers
+ 1
Do you mean variables/indexes in a string array? or number of characters in a string variable?
28th Jul 2016, 7:33 AM
James
James - avatar
+ 1
do you mean length of string?? import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); System.out.printf("Length is %s",str.length()); } }
28th Jul 2016, 9:37 AM
Manikanth Vanka
Manikanth Vanka - avatar
+ 1
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int Vcount=0; int ConCount=0; System.out.println("enter string"); String str=sc.nextLine(); for(int i=0;i<str.length();i++){ if(str.charAt(i)=='a'|| str.charAt(i)=='e'|| str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u'){ Vcount++; } else { ConCount++; } } System.out.println("vowels are "+Vcount); System.out.println("Coms are "+ConCount); System.out.println("code by ManikanthVanka"); } }
28th Jul 2016, 2:13 PM
Manikanth Vanka
Manikanth Vanka - avatar
0
number of vowels ina string variable
28th Jul 2016, 7:41 AM
honey
honey - avatar
0
Looking for the code or some guidance?
28th Jul 2016, 7:52 AM
James
James - avatar
0
looking for code
28th Jul 2016, 11:48 AM
honey
honey - avatar
0
you can also get this program from code ground in sololearn.
28th Jul 2016, 2:14 PM
Manikanth Vanka
Manikanth Vanka - avatar
0
for(int i = 0; i < str.length(); i++) if(str.charAt(i) == 'a') numVowels++; Use an if in the for loop for each vowel or use the same if by using the OR operator ||. Very quick and easy.
29th Jul 2016, 6:52 AM
nk361
nk361 - avatar