Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

Scansets ในภาษา C

Scanset (%[]) ใน scanf ระบุชุดตัวอักษรที่ยอมรับ (Case Sensitive)

ตัวอย่าง: รับเฉพาะ A-Z

scanf("%[A-Z]s", str);
Input: HElloWorld -> Output: HE (หยุดที่ 'l' เพราะเป็น Lowercase)

ตัวอย่าง: ยกเว้นตัวอักษร (^)

scanf("%[^r]s", str);
Input: HelloWorld -> Output: HelloWo (หยุดที่ 'r')

ตัวอย่าง: รับทั้งบรรทัด (รวม Space)

scanf("%[^\n]s", str);
Input: Hello World. This line has spaces.
Output: Hello World. This line has spaces.