C 語言庫 - isupper() 函式



C 的ctypeisupper() 函式用於檢查給定的字元是否為大寫字母。

語法

以下是 C 庫 isupper() 函式的語法:

int isupper(int c);

引數

此函式接受一個引數:

  • c - 要檢查的字元,作為 int 型別傳遞。c 的值應可表示為無符號字元或等於 EOF 的值。

返回值

如果字元是大寫字母(從 'A' 到 'Z'),則 isupper 函式返回非零值(true)。否則,它返回 0(false)。

示例 1:檢查單個字元

這裡,字元 'G' 使用 isupper 進行檢查,由於它是大寫字母,因此該函式返回非零值。

#include <stdio.h>
#include <ctype.h>
int main() {
    char ch = 'G';
    if (isupper(ch)) {
        printf("'%c' is an uppercase letter.\n", ch);
    } else {
        printf("'%c' is not an uppercase letter.\n", ch);
    }
    return 0;
}

輸出

執行上述程式碼後,我們得到以下結果:

'G' is an uppercase letter.

示例 2:遍歷字串

此示例遍歷字串 "Hello World!" 並計算大寫字母的數量。

#include <stdio.h>
#include <ctype.h>

int main() {
   char str[] = "Hello World!";
   int uppercase_count = 0;

   for (int i = 0; str[i] != '\0'; i++) {
      if (isupper(str[i])) {
         uppercase_count++;
      }
   }
   printf("The string \"%s\" has %d uppercase letters.\n", str, uppercase_count);
   return 0;
}

輸出

上述程式碼的輸出如下:

The string "Hello World!" has 2 uppercase letters.
廣告

© . All rights reserved.