Java Program to Find Weight of String

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:-

1) 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.

2) 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.

3) If the character is a vowel then its weight can be ignored or can be included based on the condition.

3) 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().

We have converted the given string to lowercase so that we have to write code for only lowercase. And as per 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!

Leave a Comment

Your email address will not be published.