➤ Sum of digits in a String
➤ Count No of Vowels String
➤ String Pattern Programs in Java
➤ Take String Input In Java
➤ Take Multiple String Input in Java
➤ How To Reverse a String In Java
➤ Remove Special Characters
➤ String – Remove Character
➤ String Palindrome In Java
➤ Sort String In Java
➤ How to Compare Strings In Java
➤ Second Occurrence of Character
➤ Replace nth Occurrence String
➤ Last Occurrence of Character
➤ Uppercase & Lowercase
➤ Java Check If Char is Uppercase
➤ Check If String is Uppercase
➤ Swap Characters in String Java
➤ Java String indexOf() Method
➤ How to Replace Dot in Java?
➤ How to Find Length of String
➤ Substring Method In Java
➤ Split Method In Java
Java Program to Find Weight of String | In this section, we will learn how to find the weight of a string in Java programming language.
What is the weight of a string? The weight of the string is the sum of the weight of all particular character that occurs in the string. The rules which are followed while calculating the weight of the string are as follows:-
- The uppercase and lowercase letters should be considered as same and they will have the same weight. It means the weight of A=a=1, the weight of B=b=2, the weight of C=c=3, so on, and the weight of Z=z=26.
- If the character is a non-alphabetic character (except a-z or A-Z) then its weight should be ignored. For these characters weight = 0.
- If the character is a vowel then its weight can be ignored or can be included based on the condition.
- For the remaining characters weight of each letter is its position in the English alphabet system, i.e. weight of a=1, the weight of b=2, the weight of c=3, the weight of d=4, and so on…. Similarly, the weight of y=25, the weight of z=26
How to Find Weight of String in Java
The weightOfString() method takes two arguments and if the “ignore” (second parameter) value is 0 then the weight of vowels will be ignored else it will be included.
Java Program to Find Weight Of String
public class Main {
// if ignore value is 0 then only ignore vowels
public static int weightOfString(String string, int ignore) {
if(string == null || string.isEmpty()) {
return 0;
}
// convert string to lower-case
string = string.toLowerCase();
// variable to store weight of string
int weight = 0;
for (int i = 0; i < string.length(); i++) {
char letter = string.charAt(i);
if (ignore == 0) {
if (letter == 'a' || letter == 'e' || letter == 'i'
|| letter == 'o' || letter == 'u') {
continue;
}
}
if (letter >= 97 && letter <= 122) {
weight += letter - 96;
}
}
return weight;
}
public static void main(String[] args) {
String string = "Know Program";
// ignore vowels
System.out.println("Weight of string ignoring vowels = "
+ weightOfString(string, 0));
// include vowels
System.out.println("Weight of string including vowels = "
+ weightOfString(string, 1));
}
}
Output:-
Weight of string ignoring vowels = 120
Weight of string including vowels = 151
In the above Java program to find weight of String, we have defined the Main class which has the static method weightOfString() which returns an integer value and takes two parameters string and an integer value.
First, we have checked whether the given string is null or empty. If yes then the weight of the string will be 0. Hence simply return 0 to the caller method. if you are using Java 11 then you can use isBlank() instead isEmpty(). See more:- Difference between isBlank() and isEmpty() methods of Java String class
We have converted the given string to lowercase so that we have to write code for only lowercase. And as per the rule, the lowercase and uppercase characters will have the same weight. Therefore writing separate code for each case, it is better to convert the string into either lowercase or uppercase and write code only for one case.
We have initialized the “weight” variable to 0. In the for loop, we get the character by using the charAt() method. If the “ignore” value is 0, then In the if block, we check whether the character is a vowel (‘aeiou’ or ‘AEIOU’) or not. If yes then continue, and ignore the weight.
Since string contains only lowercase therefore we have to write code only for the lowercase character. If the letter is greater than or equal to 97 or lesser than or equal to 122 add the weight of the letter and subtract by 96. Finally, return the weight. Also see:- How To Remove Substring From String Java
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!