This C Program finds the sum of the ASCII values of all characters that were used in a string.
Here is a source code of the C program to find the sum of the ASCII values of all characters that were used in a string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Find the Sum of ASCII values of all Characters
* in a String
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int sumascii(char [], int);
int main()
{
char str1[30];
int sum;
printf("Enter a string: ");
scanf("%s", str1);
sum = sumascii(str1, 0);
printf("The sum of all ascii values in '%s' is %d.\n", str1, sum);
return 0;
}
int sumascii(char str[], int num)
{
if (num < strlen(str))
{
return (str[num] + sumascii(str, num + 1));
}
return 0;
}
$ gcc asciisum.c $ ./a.out Enter a string: education The sum of all ascii values in 'education' is 956.
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms
If you wish to look at programming examples on all topics, go to C Programming Examples.