top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

Write a function that takes a string as input and calculates the weight of the string as per rules mentioned below?

+2 votes
5,290 views

Weight of vowels that appear in the string should be ignored and All non-alphabetic characters in the string should be ignored.

Weight of each letter is its position in the English alphabet system, i.e. weight of a=1, weight of b=2, weight of c=3, weight of d=4, and so on….weight of y=25, weight of z=26
Weight of Upper-Case and Lower-Case letters should be taken as the same, i.e. weight of A=a=1, weight of B=b=2, weight of C=c=3, and so on…weight of Z=z=26.

Example1:
Let us assume the word is “Hello World!!”
Weight of “Hello World!!” = 8+0+12+12+0+0+23+0+18+12+4+0+0 = 89
Note that weight of vowels is ignored. Also note that the weight of non-alphabetic characters such as space character and ! is taken as zero.

posted May 13, 2017 by Tanay Hanuman Toor

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
String givenString // read string from System.in
for(i =0 to givenString.length;i++)
int asciiOfChar = (int)givenString.charAt(i)
if ((asciiOfChar notIn(65,97,...all vowels and 91 to 96 )) && asciiOfChar In(66 to 122))
asciiOfChar = asciiOfChar In(98 to 122) ? asciiOfChar - 97 :  asciiOfChar-65
addWeight + = asciiOfChar + 1 (+1 because you are starting positioning from one)
Here I added a rough solution if you need the working code,ack me.
Hey! I am a beginner.Can you mention complete working code?

1 Answer

0 votes
public int findWeigthOfString(String input1,int input2){
    int sum=0;
    String s=input1.toUpperCase;
    int len=input1.length();
    for(int i=0;i<len;i++){
        char letter=s.charAt(i);
        if(Character.isAlphabetic(letter)){
            if((input2==0)&&(letter=='A'||letter=='E'||letter=='I'||letter=='O'||letter=='U'))
                sum+=0
            else
                sum+=letter-64;
        }
    }
    return sum;
}
answer Jun 25, 2018 by anonymous
if vowels are ignored
Thx Rinesh
Similar Questions
+1 vote

Alex has been asked by his teacher to do an assignment on sums of digits of a number. The assignment requires Alex to find the sum of sums of digits of a given number, as per the method mentioned below.

If the given number is 582109, the Sum of Sums of Digits will be calculated as =
= (5 + 8 + 2 + 1 + 0 + 9) + (8 + 2 + 1 + 0 + 9) + (2 + 1 + 0 + 9) + (1 + 0 + 9) + (0 + 9) + (9)
= 25 + 20 + 12 + 10 + 9 + 9 = 85

Alex contacts you to help him write a program for finding the Sum of Sums of Digits for any given number, using the above method.

Help Alex by completing the login in the given function sumOfSumsOfDigits which takes as input an integer input1 representing the given number.
The function is expected to return the "Sum of Sums of Digits" of input1.

Assumptions: For this assignment, let us assume that the given number will always contain more than 1 digit, i.e. the given number will always be >9.

+2 votes

Assumption:

  • The input strings will contain only numeric digits
  • The input strings can be of any large lengths
  • The lengths of the two input string need not be the same
  • The input strings will represent only positive numbers

Example

If input strings are “1234” and “56”, the output string should be “1290”
If input strings are “56” and “1234”, the output string should be “1290”
If input strings are “123456732128989543219” and “987612673489652”, the output string should be “123457719741663032871”

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...