Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Java] Sum of letters in sentence.
08-14-2009, 11:43 AM
Post: #1
[Java] Sum of letters in sentence.
A challenge from over at freelancecode (.net).

Quote:Given the following information:

a = 26, b = 25, c = 24, d = 23 ..... x = 3, y = 2, z = 1

What is the sum of each letter of this sentence: "The quick brown fox jumped over the cow"? (Note, a white space has no value). Program this in any language you want.

Code:
/**
* @(#)Sum_of_letters_in_a_sentence.java
*
* Sum_of_letters_in_a_sentence application
*
* @author
* @version 1.00 2009/8/14
*/

public class Sum_of_letters_in_a_sentence {
    
    public static void main(String[] args) {
        
        int A = 26, a = 26;
        int B = 25, b = 25;
        int C = 24, c = 24;
        int D = 23, d = 23;
        int E = 22, e = 22;
        int F = 21, f = 21;
        int G = 20, g = 20;
        int H = 19, h = 19;
        int I = 18, i = 18;
        int J = 17, j = 17;
        int K = 16, k = 16;
        int L = 15, l = 15;
        int M = 14, m = 14;
        int N = 13, n = 13;
        int O = 12, o = 12;
        int P = 11, p = 11;
        int Q = 10, q = 10;
        int R = 9, r = 9;
        int S = 8, s = 8;
        int T = 7, t = 7;
        int U = 6, u = 6;
        int V = 5, v = 5;
        int W = 4, w = 4;
        int X = 3, x = 3;
        int Y = 2, y = 2;
        int Z = 1, z = 1;
        
        System.out.println(T + h + e + q + u + i + c + k + b + r + o + w + n + f + o + x + j + u + m + p + e + d + o + v + e + r + t + h + e + c + o + w);
        
        
    }
}
Find all posts by this user
Quote this message in a reply
08-14-2009, 12:19 PM (This post was last modified: 08-14-2009 01:10 PM by G4143.)
Post: #2
RE: [Java] Sum of letters in sentence.
I'll take a look at this Tonight or tomorrow. The solution will be in C...G4143
I think this will meet the requirements. I got a total of 450...G4143

Code:
#include <stdio.h>
#include <stdlib.h>

int mystrlen(char *s)
{
    int i = 0;
    while (*s++)
        ++i;
    return i;
}

char ch[] = "The quick brown fox jumped over the cow";

int main(int argc, char**argv)
{
    int i = 0, tot = 0;
    int len = mystrlen(ch);

    for (i = 0; i < len; ++i)
    {
        if (ch[i] > 64 && ch[i] < 91)
        {
            ch[i] = 91 - ch[i];
        }
        else if (ch[i] > 96 && ch[i] < 123)
        {
            ch[i] = 123 - ch[i];    
        }
        else
        {
            ch[i] = 0;
        }
    }

    for (i= 0; i < len; ++i)
    {
        tot += ch[i];
    }

    fprintf(stdout, "total->%d\n", tot);

    exit(EXIT_SUCCESS);    
}
Find all posts by this user
Quote this message in a reply
08-14-2009, 05:44 PM
Post: #3
RE: [Java] Sum of letters in sentence.
(08-14-2009 11:43 AM)Sketch Wrote:  A challenge from over at freelancecode (.net).

Quote:Given the following information:

a = 26, b = 25, c = 24, d = 23 ..... x = 3, y = 2, z = 1

What is the sum of each letter of this sentence: "The quick brown fox jumped over the cow"? (Note, a white space has no value). Program this in any language you want.

Code:
/**
* @(#)Sum_of_letters_in_a_sentence.java
*
* Sum_of_letters_in_a_sentence application
*
* @author
* @version 1.00 2009/8/14
*/

public class Sum_of_letters_in_a_sentence {
    
    public static void main(String[] args) {
        
        int A = 26, a = 26;
        int B = 25, b = 25;
        int C = 24, c = 24;
        int D = 23, d = 23;
        int E = 22, e = 22;
        int F = 21, f = 21;
        int G = 20, g = 20;
        int H = 19, h = 19;
        int I = 18, i = 18;
        int J = 17, j = 17;
        int K = 16, k = 16;
        int L = 15, l = 15;
        int M = 14, m = 14;
        int N = 13, n = 13;
        int O = 12, o = 12;
        int P = 11, p = 11;
        int Q = 10, q = 10;
        int R = 9, r = 9;
        int S = 8, s = 8;
        int T = 7, t = 7;
        int U = 6, u = 6;
        int V = 5, v = 5;
        int W = 4, w = 4;
        int X = 3, x = 3;
        int Y = 2, y = 2;
        int Z = 1, z = 1;
        
        System.out.println(T + h + e + q + u + i + c + k + b + r + o + w + n + f + o + x + j + u + m + p + e + d + o + v + e + r + t + h + e + c + o + w);
        
        
    }
}

Ah that's exactly what i did in pascal basically lol
apparently this way isn't the best i like G4143's way the best

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
08-14-2009, 05:59 PM (This post was last modified: 08-14-2009 06:00 PM by drdebcol.)
Post: #4
RE: [Java] Sum of letters in sentence.
Interesting programming assignment ! Sketch did it in java using, i can say, bruteforce writing, because he wrote everything and summed it up !
G4143 did it in C++ using ASCII and return functions from it - i like it !
Now i want to add my solution in Pascal which is not like two past solutions,
i used set of checking and char loop ! Most of Pascal programmers don't know this exist :
Code:
program sum_of_letters;
uses wincrt;
var
br,i:integer;
s:string;
function letter(a:char):integer;
var
   i:char;
   br:integer;
begin
   br:=0;
   if a in ['A'..'Z'] then
     begin
        for i:='A' to 'Z' do
          begin
             br:=br+1;
             if a=i then
               letter:=27-br;
          end;
     end
   else
     begin
     for i:='a' to 'z' do
          begin
             br:=br+1;
             if a=i then
             letter:=27-br;
          end;
     end;
   end;
begin
s:='The quick brown fox jumped over the cow';
br:=0;
for i:=1 to length(s) do
   if not (copy(s,i,1)=' ') then
     br:=br+letter(s[i]);
writeln(br);
end.
And yeah, solution is 450 !

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
08-14-2009, 10:20 PM
Post: #5
RE: [Java] Sum of letters in sentence.
Yeah, the best way would be to use a loop, and assign each letter a value through the loop.
Then print out the sum of it all.

And yep, my answer was also 450 Wink

Keep it up guys!
Find all posts by this user
Quote this message in a reply
09-23-2009, 11:06 AM
Post: #6
RE: [Java] Sum of letters in sentence.
There's an easier way to do it. The sentence 'the quick brown fox jumps over the lazy dog' uses each letter of the alphabet once. Only once. Knowing that, you could've used a for() loop and just added the values from 1 to 26.
Find all posts by this user
Quote this message in a reply
09-23-2009, 06:41 PM
Post: #7
RE: [Java] Sum of letters in sentence.
Thats well spotted there. I don't code Java now anymore. Into C#.
But thanks!

Find all posts by this user
Quote this message in a reply
12-09-2009, 08:14 AM
Post: #8
RE: [Java] Sum of letters in sentence.
I think I'd probably do this one like oToom. Tongue
Now a challenge would be to make it for any entered sentence.
I will do it when I get the opportunity. Smile

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
12-09-2009, 08:28 AM
Post: #9
RE: [Java] Sum of letters in sentence.
I did a java and c++ solution here. http://www.freelancecode.net/community/v...?f=13&t=11
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: