How can automatic conversion of C language string ASCII codes be achieved?
To convert characters in a string to ASCII codes, you can use character arrays and loop structures in the C language.
Below is an example code:
#include <stdio.h>
int main() {
char str[100];
printf("请输入字符串:");
scanf("%s", str);
int i = 0;
while(str[i] != '\0') {
printf("字符 %c 对应的ASCII码为 %d\n", str[i], str[i]);
i++;
}
return 0;
}
In this code, a character array named str is first defined to store the input string. Then, the scanf function is used to get the string from user input. A while loop is then used to iterate through each character in the string until the end-of-string character \0 is encountered.
Within the loop, you can access each character in the string using str[i] and display the character and its corresponding ASCII code using %c and %d formatting for the characters.
Run the program, input a string, and then it will print out the ASCII code for each character in the string one by one.