Martin Dimitrov
Martin Dimitrov

Reputation: 359

C# Splitting a word into characters and then summing the ASCII code of each letter to get a sum

how can I split a word into characters and then sum all of the ASCII codes to get an integer sum?

Upvotes: 0

Views: 1629

Answers (1)

Christos
Christos

Reputation: 53958

Uisng LINQ, this can be done as below (input is your string):

var sum = input.Sum(ch=>(int)ch);

Otherwise you could use a foreach statement and loop through the string's characters:

var sum = 0;
foreach(var ch in input)
{
    sum += (int)ch;
}

Upvotes: 6

Related Questions