0
Write a java program for count a num of variables in a string.
8 odpowiedzi
+ 1
Do you mean variables/indexes in a string array? or number of characters in a string variable?
+ 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());
}
}
+ 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");
}
}
0
number of vowels ina string variable
0
Looking for the code or some guidance?
0
looking for code
0
you can also get this program from code ground in sololearn.
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.