Write a C program to print the ASCII value of all characters in a string.
#include <stdio.h>
#include <string.h>
int main() {
char string[256];
int i = 0;
/* get the input string from the user */
printf("Enter your input string:");
fgets(string, 256, stdin);
string[strlen(string) - 1] = '\0';
/* prints ascii value of all characters in the i/p string */
while (string[i] != '\0') {
printf("Character: %c\tASCII: %d\n",
string[i], string[i]);
i++;
}
return 0;
}
#include <string.h>
int main() {
char string[256];
int i = 0;
/* get the input string from the user */
printf("Enter your input string:");
fgets(string, 256, stdin);
string[strlen(string) - 1] = '\0';
/* prints ascii value of all characters in the i/p string */
while (string[i] != '\0') {
printf("Character: %c\tASCII: %d\n",
string[i], string[i]);
i++;
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input string:HelloWorld
Character: H ASCII: 72
Character: e ASCII: 101
Character: l ASCII: 108
Character: l ASCII: 108
Character: o ASCII: 111
Character: W ASCII: 87
Character: o ASCII: 111
Character: r ASCII: 114
Character: l ASCII: 108
Character: d ASCII: 100
Enter your input string:HelloWorld
Character: H ASCII: 72
Character: e ASCII: 101
Character: l ASCII: 108
Character: l ASCII: 108
Character: o ASCII: 111
Character: W ASCII: 87
Character: o ASCII: 111
Character: r ASCII: 114
Character: l ASCII: 108
Character: d ASCII: 100
No comments:
Post a Comment