Basic Problem 5. "Reverse Sentence"

เรียนรู้ array

รับประโยคภาษาอังกฤษมา แล้วให้ปริ้นคำย้อนจากหลังมาหน้า เช่น

std_input: "sentence is a set of words that is complete in itself."
std_output: "itself. in complete is that words of set a is sentence"
// reverse_sentence.cpp
#include <stdio.h>

char input[100]; // เตรียมกระดาษทด ขนาดใหญ่พอจะทดตัวอักษร 100 ตัวได้
int main() {

  int i = 0;
  // รับ input ทีละ 1 character ไปเรื่อยๆจนว่าจะ จบบรรทัด (\n = new line charactor, ขึ้นบรรทัดใหม่, เคาะ enter)
  while (scanf("%c", &input[i]) == 1 && input[i] != '\n') {
    i += 1;
  }
  int input_size = i; // ทดจำนวนอักษรของประโยคทั้งหมดไว้
  input[input_size] = '\0'; // ถ้าจะปริ้น %s จะให้ปริ้นจนถึงตัว \0
  for (int start = input_size - 1; start >= 0; start -= 1) { // ไล่ย้อนทำจากข้างหลัง
    if (input[start] == ' ') { // แสดงผลถ้าจบคำแล้ว (เจอ space)
      printf("%s ", &input[start + 1]);
      input[start] = '\0'; // กำหนดจุดสิ้นสุดการปริ้นใหม่
    } else if (start == 0) { // หรือแสดงผลถ้าเป็นตัวแรก
      printf("%s", &input[start]);
    }
  }
  printf("\n");
  return 0;
}

อธิบายเพิ่มเติมตามลำดับ

ช่วง input
i  ━━━━━━┓
input = [?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?]
i  ━━━━━━┓
input = [s,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?]
i  ━━━━━━━━┓
input = [s,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?]
i  ━━━━━━━━┓
input = [s,e,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?]
i  ━━━━━━━━━━┓
input = [s,e,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?]
i  ━━━━━━━━━━┓
input = [s,e,n,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?]
i  ━━━━━━━━━━━━┓
input = [s,e,n,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?]
i  ━━━━━━━━━━━━┓
input = [s,e,n,t,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?]
i  ━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?]
i  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?]
i  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?]
i  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,?,?,?,?,?,?,?]
i  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,?,?,?,?,?,?,?]
i  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,.,?,?,?,?,?,?]
i  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,.,?,?,?,?,?,?]
i  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ (ค่าเป็น '\n' หยุดทำงานแค่นี้)
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,.,?,?,?,?,?,?]

i  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ (ต่อมากำหนดให้เป็น '\0')
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,.,0,?,?,?,?,?]

ไล่ย้อนกลับมา

start  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,.,0,?,?,?,?,?]

เจอ ' ' ปริ้นตั้งแต่ตัวที่ input[start+1] ไปจนเจอ '\0' เป็นคำว่า "itself."

start  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ (เจอ space ให้ปริ้น "itself.")
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n, ,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ (กำหนดค่าเป็น '\0')
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ (เจอ space ให้ปริ้น "in")
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ (กำหนดค่าเป็น '\0')
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e,0,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ (เจอ space ให้ปริ้น "complete")
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s, ,c,o,m,p,l,e,t,e, ,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ (กำหนดค่าเป็น '\0')
input = [s,e,n,t,e,n,c,e, ,i,s, ,a, ,s,e,t, ,o,f, ,w,o,r,d,s, ,t,h,a,t, ,i,s,0,c,o,m,p,l,e,t,e,0,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━━━━━━━┓ (เจอ space ให้ปริ้น "a")
input = [s,e,n,t,e,n,c,e, ,i,s, ,a,0,s,e,t,0,o,f,0,w,o,r,d,s,0,t,h,a,t,0,i,s,0,c,o,m,p,l,e,t,e,0,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━━━━━━━┓ (กำหนดค่าเป็น '\0')
input = [s,e,n,t,e,n,c,e, ,i,s,0,a,0,s,e,t,0,o,f,0,w,o,r,d,s,0,t,h,a,t,0,i,s,0,c,o,m,p,l,e,t,e,0,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━┓ (เจอ space ให้ปริ้น "is")
input = [s,e,n,t,e,n,c,e, ,i,s,0,a,0,s,e,t,0,o,f,0,w,o,r,d,s,0,t,h,a,t,0,i,s,0,c,o,m,p,l,e,t,e,0,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━━━┓ (กำหนดค่าเป็น '\0')
input = [s,e,n,t,e,n,c,e,0,i,s,0,a,0,s,e,t,0,o,f,0,w,o,r,d,s,0,t,h,a,t,0,i,s,0,c,o,m,p,l,e,t,e,0,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e,0,i,s,0,a,0,s,e,t,0,o,f,0,w,o,r,d,s,0,t,h,a,t,0,i,s,0,c,o,m,p,l,e,t,e,0,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e,0,i,s,0,a,0,s,e,t,0,o,f,0,w,o,r,d,s,0,t,h,a,t,0,i,s,0,c,o,m,p,l,e,t,e,0,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━━━━━━━━━┓
input = [s,e,n,t,e,n,c,e,0,i,s,0,a,0,s,e,t,0,o,f,0,w,o,r,d,s,0,t,h,a,t,0,i,s,0,c,o,m,p,l,e,t,e,0,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━━━┓
input = [s,e,n,t,e,n,c,e,0,i,s,0,a,0,s,e,t,0,o,f,0,w,o,r,d,s,0,t,h,a,t,0,i,s,0,c,o,m,p,l,e,t,e,0,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━┓
input = [s,e,n,t,e,n,c,e,0,i,s,0,a,0,s,e,t,0,o,f,0,w,o,r,d,s,0,t,h,a,t,0,i,s,0,c,o,m,p,l,e,t,e,0,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]
start  ━━┓ (เป็นตำแหน่ง space == 0 ให้ปริ้น "sentence")
input = [s,e,n,t,e,n,c,e,0,i,s,0,a,0,s,e,t,0,o,f,0,w,o,r,d,s,0,t,h,a,t,0,i,s,0,c,o,m,p,l,e,t,e,0,i,n,0,i,t,s,e,l,f,.,0,?,?,?,?,?]

อะไรคือ char input[100];

char input[100]; มันคือการประกาศ array หรือเตรียมพื้นที่ข้อมูลที่ข้อมูลอยู่ติดๆกัน. เปรียบเหมือนเตรียมกระดาษทด ขนาดใหญ่ๆยาวๆพอจะทดตัวอักษร 100 ตัวได้

เวลาเข้าถึงให้ใช้ operation [] เช่น char[7] เหมือน veriable ทั้วๆไปเลย

// char[]
char str[20] = {'H', 'e', 'l', 'l', '0', '\0'};
char str[20] = "Hello"; // เขียนย่อๆ ได้เหมือนกัน
str[0] == 'H'
str[1] == 'e'
str[2] == 'l'
str[3] == 'l'
str[4] == '0'
str[5] == '\0'
str[-1] == ? หรือ error index out of bound
str[1000] == ? หรือ error index out of bound
  1. char[] มันถูกเรียกย่อๆว่า string

  2. operator arr[x] มีความหมายเดียวกันกับ *(arr + x) คือ

    • arr = ตำแหน่งใน memory ของพื้นที่ๆจองไว้ช่องแรก
    • arr + x = ขยับตำแหน่งนั้นไป x ช่อง ได้ตำแหน่งใหม่ที่ขยับไป x
    • *(arr + x) = แปลงจาก ตำแหน่ง ให้เป็น ค่า
    • arr[x] = ค่า ณ ตำแหน่งช่องที่ x

    เลยเป็นเหตุผลว่าทำไม index ถึงเริ่มที่ 0. เพราะว่า 0 เป็น identity การบวก

  3. ถ้าเราประกาศ int arr[10] เราจะได้ pointer ชี้ไปหา int ตัวแรกที่จองไว้

    เพราะงั้น arr[0] == *arr เกือบทุกกรณี

// int[]
int factorial[20];
factorial[0] = 1;
for (int i = 1; i < 20; i += 1) {
  factorial[i] = i * factorial[i-1];
}
factorial[0] == 1
factorial[1] == 1
factorial[2] == 2
factorial[3] == 6
factorial[4] == 24
factorial[5] == 120
factorial[6] == 720
factorial[7] == 5040
factorial[8] == 40320
factorial[9] == 362880
factorial[10] == 3628800
factorial[11] == 39916800
factorial[12] == 479001600
factorial[13] == 1932053504 (ค่าควรเป็น         6227020800 แต่ว่า int เก็บได้ไม่เกิน 32 bit)
factorial[14] == 1278945280 (ค่าควรเป็น        87178291200 แต่ว่า int เก็บได้ไม่เกิน 32 bit)
factorial[15] == 2004310016 (ค่าควรเป็น      1307674368000 แต่ว่า int เก็บได้ไม่เกิน 32 bit)
factorial[16] == 2004189184 (ค่าควรเป็น     20922789888000 แต่ว่า int เก็บได้ไม่เกิน 32 bit)
factorial[17] == -288522240 (ค่าควรเป็น    355687428096000 แต่ว่า int เก็บได้ไม่เกิน 32 bit)
factorial[18] == -898433024 (ค่าควรเป็น   6402373705728000 แต่ว่า int เก็บได้ไม่เกิน 32 bit)
factorial[19] == 109641728  (ค่าควรเป็น 121645100408832000 แต่ว่า int เก็บได้ไม่เกิน 32 bit)

การประกาศ array ประกาศหลายมิติได้นะ

// int[][][]
int data[2][3]; // array หลายมิติ
ข้อมูลจะติดๆกันแบบนี้ [data[0][0], data[0][1], data[0][2], data[1][0], data[1][1], data[1][2]]
ข้อมูลจะติดๆกันแบบนี้ [
  data[0][0], data[0][1], data[0][2],
  data[1][0], data[1][1], data[1][2]
]

ตัวอย่างการทำ array หลายมิติ (Advance เกินไปไม่ออกสอบ)

ค่าทั้งหมดในนี้มีค่าเท่ากันสำหรับ `int data[X][Y];`
= data[i][j]
= *(data[0][i * Y + j])
= *(data[i] + j)
= *(data[0] + 3 * Y + j)
= *(&data[0][0] + 3 * Y + j)
= *(&**data + 3 * Y + j)
= (*&data)[i][j] // `&` แปลง variable เป็นตำแหน่ง และ `*` แปลงตำแหน่งกลับเป็นค่า
= (*&*&data)[i][j]
= (*&*&*&*&*&*&*&data)[i][j]
ค่าทั้งหมดในนี้มีค่าเท่ากันสำหรับ `int data[X][Y][Z];`
= data[i][j][k]
= data[i][0][j*Z + k]
= data[0][i*Y][j*Z + k]
= data[0][0][i*Y*Z + j*Z + k]
= *(&data[0][0][0] + i*Y*Z + j*Z + k)
= *(&***data + i*Y*Z + j*Z + k)
= *(**data + i*Y*Z + j*Z + k)

ลองเขียนใหม่โดยใช้ function จาก library มากขึ้น

function strlen https://cplusplus.com/reference/cstring/strlen เป็น function โง่ๆที่จะ #include เอาหรือเขียนเองก็ได้

#include <stdio.h>

// string length :: string -> int
// มีใน #include <string.h> เหมือนกัน
int strlen(char* c) {
  int i = 0;
  while (c[i] !== '\0') i += 1;
  return i;
}

char input[100];
int main() {
  input[0] = ' ';
  fgets(input + 1, 100, stdin); // get line โดยเขียนค่าเริ่มจาก input[1..] (ไม่ต้องจำ ไม่ค่อยได้ใช้)
  int i = strlen(input); // get string length
  while (input[i - 1] == '\n') input[--i] = '\0'; // remove new line
  while (--i >= 0) {
    if (input[i] == ' ') {
      printf("%s ", input + i + 1);
      input[i] = '\0';
    }
  }
  printf("\n");
  return 0;
}