C Program to Find the Sum of ASCII Value of All Characters in the String

This is a C Program to find the sum of ASCII values of all characters in a given string.

Problem Description

This program takes a string as input and finds the sum of ASCII values of all characters in a given string.

Problem Solution

1. Take a string as input and store it in the array.
2. Add all the elements of the array.
3. Print the output and exit.

Program/Source Code

Here is source code of the C Program to find the sum of ASCII values of all characters in a given string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1.  
  2. /* 
  3.  * C Program To Find the Sum of ASCII values of All Characters in a 
  4.  * given String
  5.  */
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. void main()
  10. {
  11.     int sum = 0, i, len;
  12.     char string1[100];
  13.  
  14.     printf("Enter the string : ");
  15.     scanf("%[^\n]s", string1);
  16.         len = strlen(string1);
  17.     for (i = 0; i < len; i++)
  18.     {
  19.         sum = sum + string1[i];
  20.     }
  21.     printf("\nSum of all characters : %d ",sum);
  22. }
Program Explanation

1. Take a string as input and store it in the array string[].
2. Initialize the variable sum to zero. Using for loop increment the variable sum with the elements of the array.
3. Print the variable sum as output.

advertisement
advertisement
Runtime Test Cases
Enter the string : Welcome to Sanfoundry's C Programming Class, Welcome Again to C Class !
 
Sum of all characters : 6307

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.