ASCII Value Program in C

Ascii value of a character is the numerical value associated with it. Each Character has a unique ascii value ranging from 0-127 (128 characters in total).

Examples:

Ascii Value of ‘a’ is: 97
Ascii Value of ‘A’ is: 65

Problem Description

Write a C program to find the ascii value of a character or a string.

Problem Solution

1. Take an input from the user. (Character/String)
2. Print Ascii value by typecasting the character to integer.

There are several ways to find the ASCII value of a character or string in C language. Let’s take a detailed look at all the approaches to find the ASCII value of a character.

advertisement
advertisement
Method 1: Find ASCII Value of a Character in C

In this approach, we find the ascii value of a character by simply type casting it to an integer.

Example:
Input:
Enter Character: c
Output:
Ascii Value of ‘c’ is: 99

Program/Source Code

Here is source code of the C Program to find the ascii value of a character. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1. /*
  2.  * C Program to find the ascii value of a character
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.     char ch;
  10.     printf("Enter Character: ");
  11.     scanf("%c",&ch);
  12.  
  13.     //Type Casting character to int
  14.     int ascii = (int)ch;
  15.     printf("Ascii Value of %c is %d",ch,ascii);
  16.     return 0;
  17. }
Program Explanation

1. Take a character from the user as input.
2. Store the character in variable ch.
3. Print its ascii value by type casting the character to int.

Note: Join free Sanfoundry classes at Telegram or Youtube

Time Complexity: O(1)
The time complexity of basic input, output and typecasting is O(1).

Space Complexity: O(1)
Since no auxiliary space is required, space complexity is O(1).

Runtime Test Cases

Here is the runtime output of the C program to find the ascii value of a character when the user entered character is “c”.

Enter Character: c
Ascii Value of c is 99

advertisement
Method 2: Find the ASCII Value of All Characters in a String

In this approach, we find the ascii value of all characters in a string by simply typecasting each character to an integer.

Example:
Input:
Enter String: hello

Output:
Ascii value of ‘h’ is: 104
Ascii value of ‘e’ is: 101
Ascii value of ‘l’ is: 108
Ascii value of ‘l’ is: 108
Ascii value of ‘o’ is: 111

Program/Source Code

Here is a source code of the C Program to find the ascii value of all characters in a string. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

advertisement
  1. /*
  2.  * C Program to find the ascii value of all characters in a string
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.     char str[100];
  10.  
  11.     //Input string
  12.     printf("Enter String: ");
  13.     gets(str);
  14.     int i=0;
  15.  
  16.     //Iterating over string
  17.     while(str[i]!='\0')
  18.     {
  19.         //Printing ascii value by typecasting
  20.         printf("Ascii value of %c : %d\n",str[i],(int)str[i]);
  21.         ++i;
  22.     }
  23.     return 0;
  24. }
Program Explanation

1. Take a string from the user as input.
2. Traverse the string until we encounter a backspace character (which marks the end of the string).
3. Print the ascii value of each character by typecasting it to an integer.

Time Complexity: O(n)
Since we are traversing the whole array, time complexity is O(n), where n is length of string.

Space Complexity: O(n)
Space is required to store the string, space complexity is O(n), where n is length of string.

Runtime Test Cases

Here is the runtime output of the C program to find the ascii value of all characters in a string when the user entered string is “hello”.

Enter String: hello
Ascii value of h : 104
Ascii value of e : 101
Ascii value of l : 108
Ascii value of l : 108
Ascii value of o : 111

Method 3: ASCII Value of All Characters in a String (Advanced Approach)

In this approach, we find the ascii value of all characters in a string by simply typecasting each character to an integer.

Program/Source Code

Here is source code of the C Program to input a string & store their ascii values and print them. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1. /*
  2.  * C Program to Display ASCII value of all Characters used in the String
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8.  
  9. struct detail
  10. {
  11.     char c;
  12.     int ascii;
  13. };
  14.  
  15. int update(struct detail [], const char [], int);
  16.  
  17. int main()
  18. {
  19.     struct detail s[26];
  20.     char string[100], c;
  21.     int i = 0, index;
  22.  
  23.     for (i = 0; i < 26; i++)
  24.     {
  25.        s[i].c = i + 'a';
  26.        s[i].ascii = 0;
  27.     }
  28.     printf("Enter string: ");
  29.     i = 0;
  30.     do
  31.     {
  32.         fflush(stdin);
  33.         c = getchar();
  34.         string[i++] = c;
  35.         if (c == '\n')
  36.         {
  37.             break;
  38.         }
  39.         index = c - 'a';
  40.         s[index].ascii = c;
  41.     }
  42.     while (1);
  43.     string[i - 1] = '\0';
  44.     printf("The string entered is: %s\n", string);
  45.  
  46.     printf("*************************\nCharacter\tASCII\n*************************\n");
  47.     for (i = 0; i < 26; i++)
  48.     {
  49.         if (s[i].ascii)
  50.         {
  51.             printf("     %c\t\t   %d\n", s[i].c, s[i].ascii);
  52.         }
  53.     }
  54.  
  55.     return 0;
  56. }

Time Complexity: O(n)
Since we are traversing the whole array, time complexity is O(n), where n is length of string.

Space Complexity: O(n)
Space is required to store the string, space complexity is O(n), where n is length of string.

Runtime Test Cases

Here is the runtime output of the C program to find the ascii value of all characters in a string when the user entered string is “Sanfoundry ascii program”.

Enter string: Sanfoundry ascii program
The string entered is: Sanfoundry ascii program
*************************
Character	ASCII
*************************
     a		   97
     c		   99
     d		   100
     f		   102
     g		   103
     i		   105
     m		   109
     n		   110
     o		   111
     p		   112
     r		   114
     s		   115
     u		   117
     y		   121

Method 4: Find the Sum of ASCII Values of All Characters in a String

In this approach, it take input from the user and extract ascii values in the given string as shown in the program below.

Example:
Input:
Enter a string: education

Output:
Ascii value of ‘e’ is: 101
Ascii value of ‘d’ is: 100
Ascii value of ‘u’ is: 117
Ascii value of ‘c’ is: 99
Ascii value of ‘a’ is: 97
Ascii value of ‘t’ is: 116
Ascii value of ‘i’ is: 105
Ascii value of ‘o’ is: 111
Ascii value of ‘n’ is: 110
The sum of all ascii values in ‘education’ is 956.

Program/Source Code

Here is source code of the C Program to input a string & store their ascii values and print them. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1. /*
  2.  * C Program to Display ASCII value of all Characters used in the String
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8.  
  9. int sumascii(char [], int);
  10.  
  11. int main()
  12. {
  13.     char str1[30];
  14.     int sum;
  15.  
  16.     printf("Enter a string: ");
  17.     scanf("%s", str1);
  18.     sum = sumascii(str1, 0);
  19.     printf("The sum of all ascii values in '%s' is %d.\n", str1, sum);
  20.  
  21.     return 0;
  22. }
  23.  
  24. int sumascii(char str[], int num)
  25. {
  26.     if (num < strlen(str))
  27.     {
  28.         return (str[num] + sumascii(str, num + 1));
  29.     }
  30.  
  31.     return 0;
  32. }
Program Explanation

1. In this C program, reading the value of string using ‘string1’ variable.
2. Compute the length of the string using strlen() function.
3. Initialize the value of ‘i’ variable value as 0 and check the value of ‘i’ variable is less than the value of ‘len’ variable.
4. If the condition is true then execute the statement and compute the summation of the value of ‘sum’ variable with the value of string1[] array variable.
5. Print the sum of ASCII values of all characters in a given string.

Runtime Test Cases

Here is the runtime output of the C program to find the sum of ascii value of all characters in a string when the user entered string is “education”.

 
$ gcc asciisum.c 
$ ./a.out
Enter a string: education
The sum of all ascii values in 'education' is 956.

Method 5: Find the Sum of ASCII Values of All Characters in a String using For Loop

In this approach, it take input from the user and extract ascii values in the given string using for loop.

Program/Source Code

Here is source code of the C Program to find the sum of ascii values of all characters in a string using for loop. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

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

Runtime Test Cases

Here is the runtime output of the C program to find the sum of ascii value of all characters in a string when the user entered string is “Welcome to Sanfoundry’s C Programming Class, Welcome Again to C Class !”.

 
$ gcc asciisum.c 
$ ./a.out
Enter the string : Welcome to Sanfoundry's C Programming Class, Welcome Again to C Class !
 
Sum of all characters : 6307

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

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.