top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Concatenate the sums of each word to form the result WORLD WIDE WEB = 402326?

+5 votes
6,948 views

Crazy Zak has designed the below steps which can be applied on any given string (sentence) to produce a number.
In each word, find the Sum of the Difference between the first letter and the last letter, second letter and the penultimate letter, and so on till the center of the word.
Concatenate the sums of each word to form the result.
If the given string is “WORLD WIDE WEB”
In each word, find the Sum of the Difference between the first letter and the last letter, second letter and the penultimate letter, and so on till the center of the word.

WORLD = [W-D]+[O-L]+[R] = [23-4]+[15-12]+[18] = [19]+[3]+[18] = [40]

WIDE = [W-E]+[I-D] = [23-5]+[9-4] = [18]+[5] = [23]

WEB = [W-B]+[E] = [23-2]+[5] = [21]+[5] = [26]
Concatenate the sums of each word to form the result
[40] [23] [26]
[402326]

The answer (output) should be the number 402326.

NOTE1: The value of each letter is its position in the English alphabet system i.e. a=A=1, b=B=2, c=C=3, and so on till z=Z=26.

So, the result will be the same for “WORLD WIDE WEB” or “World Wide Web” or “world wide web” or any other combination of uppercase and lowercase letters.

In Step1, after subtracting the alphabets, we should use the absolute values for calculating the sum. For instance, in the below example, both [H-O] and [E-L] result in negative number -7, but the positive number 7 (absolute value of -7) is used for calculating the sum of the differences.
Hello = [H-O]+[E-L]+[L] = [8-15]+[5-12]+[12] = [7]+[7]+[12] = [26]

Assumptions: The given string (sentence) will contain only alphabet characters and there will be only one space character between any two consecutive words.
You are expected to help Zak, by writing a function that takes a string (sentence) as input, performs the above mentioned processing on the sentence and returns the result (number).

posted May 9, 2017 by Azhar Keer

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
 
Best answer
public class WORLDWIDEWEB {

public int getLetterNo(char letter){
return (int)letter-64;
}

public int diffLetter(int a, int b){
int difference=a-b;
if(difference<0)
    difference*=-1;
return difference;
}

public static void main(String[] args){
String input1="WORLD WIDE WEB";
char ch1,ch2;
int counter;
int center;
int num1,num2;
int sum=0;
int output=0;
String[] words=input1.toUpperCase().split("\\s");
String totall="";

for(String w:words){
    counter=0;
    center=w.length()/2;
    sum=0;
    while(counter<center){
        ch1=w.charAt(counter);
        ch2=w.charAt(w.length()-counter-1);
        counter++;
        num1=getLetterNo(ch1);
        num2=getLetterNo(ch2);
        sum+=diffLetter(num1,num2);
    }
    if(w.length()%2!=0)
        sum+=getLetterNo(w.charAt(center));
    totall+=sum;
}
output=Integer.valueOf(totall);


System.out.println(output);    
}

}
answer May 13, 2017 by Tanay Hanuman Toor
Wonderful thanks for the precise answer
how to call non static method on main method am getting error
WORLDWIDEWEB w = new WORLDWIDEWEB();
w.getLetterNo();
Creating the class object where the method is defined. then call inside the main method.
can you pls give this code in c langugae...
Similar Questions
+1 vote

Crazy Zak has designed the below steps which can be applied on any given string (sentence) to produce a number.
In each word, find the Sum of the Difference between the first letter and the last letter, second letter and the penultimate letter, and so on till the center of the word.
Concatenate the sums of each word to form the result.
If the given string is “WORLD WIDE WEB”
In each word, find the Sum of the Difference between the first letter and the last letter, second letter and the penultimate letter, and so on till the center of the word.
WORLD = [W-D]+[O-L]+[R] = [23-4]+[15-12]+[18] = [19]+[3]+[18] = [40]

WIDE = [W-E]+[I-D] = [23-5]+[9-4] = [18]+[5] = [23]

WEB = [W-B]+[E] = [23-2]+[5] = [21]+[5] = [26]
Concatenate the sums of each word to form the result
[40] [23] [26]
[402326]

The answer (output) should be the number 402326.

NOTE1: The value of each letter is its position in the English alphabet system i.e. a=A=1, b=B=2, c=C=3, and so on till z=Z=26.

So, the result will be the same for “WORLD WIDE WEB” or “World Wide Web” or “world wide web” or any other combination of uppercase and lowercase letters.

In Step1, after subtracting the alphabets, we should use the absolute values for calculating the sum. For instance, in the below example, both [H-O] and [E-L] result in negative number -7, but the positive number 7 (absolute value of -7) is used for calculating the sum of the differences.
Hello = [H-O]+[E-L]+[L] = [8-15]+[5-12]+[12] = [7]+[7]+[12] = [26]

Assumptions: The given string (sentence) will contain only alphabet characters and there will be only one space character between any two consecutive words.
You are expected to help Zak, by writing a function that takes a string (sentence) as input, performs the above mentioned processing on the sentence and returns the result (number).

+3 votes

Implement a code that counts the occurrences of each word in an input file and write the word along with corresponding count in an output file sorted by the words alphabetically.

Sample Input

Gaurav is  a Good Boy
Sita is a Good Girl
Tommy is  a Good Dog
Ram is a Good person

Sample Output

D:\>Java FileWordCount inputFile.txt outputFile.txt
    Boy : 1
    Dog : 1
    Gaurav : 1
    Girl : 1
    Good : 4
    Ram : 1
    Sita : 1
    Tommy : 1
    a : 4
    is : 4
    person : 1
0 votes

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.

...