網站聲明

本網站包含了各式各樣的資源,如果有侵占到您的著作權,請與本人通知,本人會立即改進。本站所有發表僅屬研究討論性質,如果有任何後果請自行負責。

2014-06-12

TQC+ C 字元字串與檔案處理 602

602.
設計說明:

1. 修改程式碼片段中的程式語法、邏輯上的錯誤,執行結果如範例圖。


參考程式碼:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main ()
  5. {
  6.  char str2[]="Apple iPod";
  7.  char str4[]="Apple iPad";
  8.  int n;
  9.  n=strcmp(str2, str4);
  10.  if (n>0)
  11.   printf("%s大於%s\n", str2, str4);
  12.  else if (n==0)//邏輯運算等於是"=="
  13.   printf("%s等於%s\n", str2, str4);
  14.  else
  15.   printf("%s小於%s\n", str2, str4);
  16.  n=strncmp(str2, str4, 5);
  17.  if (n>0)
  18.   printf("%s前五個字元大於%s前五個字元\n", str2, str4);
  19.  else if (n==0)//邏輯運算等於是"=="
  20.   printf("%s前五個字元等於%s前五個字元\n", str2, str4);
  21.  else
  22.   printf("%s前五個字元小於%s前五個字元\n", str2, str4);
  23.  system("PAUSE");
  24.  return 0;
  25. }


  • int strcmp(const char* cs, const char* ct):比較字串csct
  •  int strncmp(const char* cs, const char* ct, size_t n):比較字串csct的前 n 個字元。
  • 如果前面大於後面則是true=1,相等為0,小於則是-1。

TQC+ C 試題總整理

聲明:

這裡的範例程式碼皆由本人親自編輯,歡迎轉載本教學,但請註明本網站,尊重一下作者的心血

沒有留言:

張貼留言

歡迎留言,較舊文章需要留言審核看不到自己的留言是正常的。
若長時間無回應請使用以下聯絡方式:
填寫表單:https://forms.gle/hxxX9n4tATcFnhnk8
寄信到:happyplayblogs@gmail.com