Find the single-digit sum of the alphabetical values of a string


In order to find the single−digit sum of the alphabetical values of a string, we will explore the alphabetical values of a string and assign numerical values to letters of the alphabet. We will jump into the concept and an example to illustrate the steps involved, the algorithm behind this process, an example code implementation in C++, and finally a brief conclusion involving the significance of this technique.

Concept

The idea rotates around associating numerical values with each letter and performing arithmetic operations to calculate the single−digit sum i.e. 'A'=1 or 'a'=1, 'B'=2 or 'b'=2, and so on. By converting the string to either uppercase or lowercase, we ensure consistency and eliminate any discrepancies i.e. converting into one case will help us evaluate and calculate the desired result easily and effectively. Taking the sum of the numerical values enables us to capture the cumulative value of the letters. In the end, reducing the sum to a single digit simplifies and offers the required result.

Algorithm

  • Take a string as an input.

  • Convert the string to either uppercase or lowercase to ensure and maintain consistency.

  • Assign numerical values to each alphabet of the input string → 'A' corresponds to 1, 'B' corresponds to 2, 'C' corresponds to 3, and so on until 'Z' corresponds to 26.

  • By iterating through each letter in the string, calculate the sum of the values assigned to the letters in step 2.

  • Reduce the sum calculated in step 3 to a single digit → If the sum obtained in step 3 has more than one digit, continue reducing it by adding each digit until you reach a single−digit result.

Take the word "LIFE" as an example −

  • Transform the string into uppercase or lowercase letters.

  • Give the string's letters numerical values, such as L = 12, I = 9, F = 6, and E = 5.

  • Calculate the values' total sum: 12+9+6+5= 32

  • Simplify the total to a single digit: 3 + 2 = 5.

As a result, "LIFE" has a single−digit total of 5.

Example

Following is the implementation of the above approach in different programming languages: C, C++, Java, and Python −

#include <stdio.h>
int alphabet_sum(char* st){
   int total_sum = 0;
   // Iterate through each character in the string
   while (*st != '\0') {
      char ch = *st;
      // Convert lowercase to uppercase for maintaining consistency
      if (ch >= 'a' && ch <= 'z') {
         ch = ch - 'a' + 'A';
      }
      // Calculate the sum by subtracting the ASCII value of 'A' from the character and adding 1.
      total_sum += ch - 'A' + 1;
      // Move to the next character in the string
      st++;
   }
   // Keep summing the digits until it becomes a single digit
   while (total_sum > 9) {
      int digits_sum = 0;
      // Calculate the sum of digits
      while (total_sum != 0) {
         digits_sum += total_sum % 10;  // Calculate remainder (i.e., ones place digit)
         total_sum /= 10;  // Calculate quotient (i.e., tens place digit)
      }
      // Update the total sum
      total_sum = digits_sum;
   }
   return total_sum;
}
int main(){
   char input_str[] = "LIFE";
   int answer = alphabet_sum(input_str);
   printf("The single digit sum of the alphabetical values is: %d\n", answer);
   return 0;
}

Output

The single digit sum of the alphabetical values is: 5
#include <iostream>
#include <string>
using namespace std;

int alphabet_sum(string& st) {
   int total_sum = 0;
   for (char ch : st) {
      if (ch >= 'a' && ch <= 'z') {
         ch=toupper(ch); // Convert lowercase to uppercase for maintaining consistency
      }
      total_sum += ch - 'A' + 1; //calculates the sum by subtracting the ASCII value of 'A' from character and adding 1.
   }
   while (total_sum > 9) {
      int digits_sum = 0;
      while (total_sum != 0) {
         digits_sum += total_sum % 10; //calculates remainder i.e. ones place digit
         total_sum /= 10; //gives quotient i.e tens place digit
      }
      total_sum = digits_sum;
   }
   return total_sum;
}
int main() {
   string input_str="LIFE";
   int answer = alphabet_sum(input_str);
   cout << "The single digit sum of the alphabetical values is: " << answer << endl;
   return 0;
}	  

Output

The single digit sum of the alphabetical values is: 5
public class AlphabetSum {
   static int alphabetSum(String st) {
      int totalSum = 0;
      // Iterate through each character in the string
      for (char ch : st.toCharArray()) {
         // Convert lowercase to uppercase for maintaining consistency
         if (ch >= 'a' && ch <= 'z') {
            ch = Character.toUpperCase(ch);
         }
         // Calculate the sum by subtracting the ASCII value of 'A' from the character and adding 1.
         totalSum += ch - 'A' + 1;
      }

      // Keep summing the digits until it becomes a single digit
      while (totalSum > 9) {
         int digitsSum = 0;
         // Calculate the sum of digits
         while (totalSum != 0) {
            digitsSum += totalSum % 10;  // Calculate remainder (i.e., ones place digit)
            totalSum /= 10;  // Calculate quotient (i.e., tens place digit)
         }

         // Update the total sum
         totalSum = digitsSum;
      }
      return totalSum;
   }
   public static void main(String[] args) {
      String inputStr = "LIFE";
      int answer = alphabetSum(inputStr);
      System.out.println("The single digit sum of the alphabetical values is: " + answer);
   }
}

Output

The single digit sum of the alphabetical values is: 5
def alphabet_sum(st):
   total_sum = 0

   # Iterate through each character in the string
   for ch in st:
      # Convert lowercase to uppercase for maintaining consistency
      if 'a' <= ch <= 'z':
         ch = ch.upper()

      # Calculate the sum by subtracting the ASCII value of 'A' from the character and adding 1.
      total_sum += ord(ch) - ord('A') + 1

   # Keep summing the digits until it becomes a single digit
   while total_sum > 9:
      digits_sum = 0

      # Calculate the sum of digits
      while total_sum != 0:
         digits_sum += total_sum % 10  # Calculate remainder (i.e., ones place digit)
         total_sum //= 10  # Calculate quotient (i.e., tens place digit)

      # Update the total sum
      total_sum = digits_sum

   return total_sum

if __name__ == "__main__":
   input_str = "LIFE"
   answer = alphabet_sum(input_str)
   print("The single digit sum of the alphabetical values is:", answer)

Output

The single digit sum of the alphabetical values is: 5

Test Case

Test case → Input String = "GURU"

Explanation

  • The alphabet_sum function is called with the input string "GURU".

  • The function iterates through each character of the string, considering only alphabetic characters. For each alphabetic character encountered, it converts any lowercase characters to uppercase by using inbuilt string function to convert characters to uppercase.

    • 'G' is converted to uppercase, and its value is added to the total sum: 7 + 0 = 7

    • 'U' is converted to uppercase, and its value is added to the total sum: 7 + 21 = 28

    • 'R' is converted to uppercase, and its value is added to the total sum: 28 + 18 = 46

    • 'U' is converted to uppercase, and its value is added to the total sum: 46 + 21 = 67

  • Since the sum obtained to be 67 is not a single−digit number, thus the code enters the while loop to reduce the sum to single digit.

  • The inner while loop repeatedly adds the individual digits of the sum i.e. 6 + 7 = 13.

  • The reduced sum obtained in previous step is 13 which again goes under the process explained in previous step i.e 1+3= 4, which is a single digit answer and hence, returned from the alphabet_sum function.

  • The single−digit sum of the alphabetical values in the input string "GURU" is shown on the console as result = 4 in the main function.

Conclusion

We have explored the intriguing idea of finding the single-digit sum of sequential properties present in a string. With this method, we can easily switch from letters to numerical values and apply mathematical operations to produce a result that is compressed and clear. We have adopted the practise of changing the string to uppercase or lowercase in order to maintain uniformity. We quantify the cumulative significance of the letters by adding up their assigned values. Finally, by reducing the total to a single digit, we can compact and simplify the output. Using this method, alphabetical values contained in strings can be properly represented and analysed.

Updated on: 22-Jan-2024

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements