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
Write a C program to find the ascii value of a character or a string.
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.
- C Program to Find ASCII Value of a Character
- C Program to Find the ASCII Value of All Characters in a String
- ASCII Value of All Characters in a String (Advanced Approach)
- Find the Sum of ASCII Values of All Characters in a String
- Find the Sum of ASCII Values of All Characters in a String using For Loop
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
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.
/*
* C Program to find the ascii value of a character
*/
#include <stdio.h>
int main()
{
char ch;
printf("Enter Character: ");
scanf("%c",&ch);
//Type Casting character to int
int ascii = (int)ch;
printf("Ascii Value of %c is %d",ch,ascii);
return 0;
}
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.
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).
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
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
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.
/*
* C Program to find the ascii value of all characters in a string
*/
#include <stdio.h>
int main()
{
char str[100];
//Input string
printf("Enter String: ");
gets(str);
int i=0;
//Iterating over string
while(str[i]!='\0')
{
//Printing ascii value by typecasting
printf("Ascii value of %c : %d\n",str[i],(int)str[i]);
++i;
}
return 0;
}
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.
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
In this approach, we find the ascii value of all characters in a string by simply typecasting each character to an integer.
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.
/*
* C Program to Display ASCII value of all Characters used in the String
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct detail
{
char c;
int ascii;
};
int update(struct detail [], const char [], int);
int main()
{
struct detail s[26];
char string[100], c;
int i = 0, index;
for (i = 0; i < 26; i++)
{
s[i].c = i + 'a';
s[i].ascii = 0;
}
printf("Enter string: ");
i = 0;
do
{
fflush(stdin);
c = getchar();
string[i++] = c;
if (c == '\n')
{
break;
}
index = c - 'a';
s[index].ascii = c;
}
while (1);
string[i - 1] = '\0';
printf("The string entered is: %s\n", string);
printf("*************************\nCharacter\tASCII\n*************************\n");
for (i = 0; i < 26; i++)
{
if (s[i].ascii)
{
printf(" %c\t\t %d\n", s[i].c, s[i].ascii);
}
}
return 0;
}
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.
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
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.
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.
/*
* C Program to Display ASCII value of all Characters used in the 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;
}
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.
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.
In this approach, it take input from the user and extract ascii values in the given string using for loop.
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.
/*
* C Program to Find the Sum of ASCII Values of All Characters in a String using For Loop
*/
#include <stdio.h>
#include <string.h>
void main()
{
int sum = 0, i, len;
char string1[100];
printf("Enter the string : ");
scanf("%[^\n]s", string1);
len = strlen(string1);
for (i = 0; i < len; i++)
{
sum = sum + string1[i];
}
printf("\nSum of all characters : %d ",sum);
}
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.
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”.
- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Apply for Computer Science Internship
- Check C Books
- Apply for C Internship