การประกาศสตริง
char stringname[size];
เช่น char string[50]; สตริงยาว 50 ตัวอักษร
การเริ่มต้นค่า
- ใช้ค่าคงที่ตัวอักษรเดี่ยว:
char string[10] = {'H','e','l','l','o','\0'}; - ใช้ค่าคงที่สตริง:
char string[10] = "Hello";
การเข้าถึง
ใช้ control string %s เข้าถึงจนกว่าจะเจอ \0
ตรรกะพิมพ์ค่า ASCII
while(str[i] != '\0') {
printf("\nASCII Value of %c = %d", str[i], str[i]);
i++;
}ตัวอย่างโค้ด
#include <stdio.h>
int main() {
char string[50];
int i = 0;
printf("Enter the Sentence: ");
gets(string);
while(string[i] != '\0') {
printf("\nASCII Value of %c=%d", string[i], string[i]);
i++;
}
getch();
return 0;
}ผลลัพธ์
Enter the Sentence: Tutorials Point ASCII Value of T = 84 ASCII Value of u = 117 ASCII Value of t = 116 ASCII Value of o = 111 ASCII Value of r = 114 ASCII Value of i = 105 ASCII Value of a = 97 ASCII Value of l = 108 ASCII Value of s = 115 ASCII Value of = 32 ASCII Value of P = 80 ASCII Value of o = 111 ASCII Value of i = 105 ASCII Value of n = 110 ASCII Value of t = 116