วันอาทิตย์ที่ 9 พฤศจิกายน พ.ศ. 2557

Try write a function to print only the first the Words from a sentence(String)

void setup () {
  String sentense = "My Name Is Aey";
  TheFirstWord (sentense);
}
void TheFirstWord (String a) {
  char space = ' ';
  for (int i = 0; i<a.length ()-1; i++) {
    if (a.charAt(0)==space) {
      print(a.charAt(i+1));
      if (a.charAt(i+2)==space) {
        break;
      }
    } else { //เอาไว้กันความผิดพลาดในกรณีที่มีการเว้นวรรคตัวแรก
      print(a.charAt(i));
      if (a.charAt(i+1)==space) {
        break;
      }
    }
  }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น