C/C++庫函數使用———ctype.h(5)

庫裏的函數:int ispunct ( int c )

使用:檢查c是否是標點字符。標準“C”語言環境將標點符號視爲非字母數字的所有圖形字符(如isgraph中所示)(如isalnum中所示)。其他語言環境可能會將不同的字符選擇視爲標點字符,但無論如何它們都是isgraph而不是isalnum。如果確實c是標點符號 ,則值不爲零(即,爲真)。否則爲零(即假)

例子

/* ispunct example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  int cx=0;
  char str[]="Hello, welcome!";
  while (str[i])
  {
    if (ispunct(str[i])) cx++;
    i++;
  }
  printf ("Sentence contains %d punctuation characters.\n", cx);
  return 0;
}

結果輸出:這個句子有兩個標點符號“,”與“!” 

例子解釋:str中只有“,”和“!”是標點符號,空格不是

庫裏的函數:int isspace ( int c )

使用:檢查c是否爲空格字符。對於“C”語言環境,空格字符是以下任意一種:

'' (0×20) 空間(SPC)
'\ t' (0×09) 水平標籤(TAB)
'\ n' (0X0A) 換行(LF)
'\ V' (0x0B中) 垂直標籤(VT)
'\F' (0x0c) 飼料(FF)
'\ r' 符(0x0D) 回車(CR)

其他語言環境可能會將不同的字符選擇視爲空格,但從不會爲isalnum返回true 。

例子

/* isspace example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
  char c;
  int i=0;
  char str[]="Example sentence to test isspace\n function";
  while (str[i])
  {
    c=str[i];
    if (isspace(c)) c='\n';
    putchar (c);
    i++;
  }
  return 0;
}

結果輸出:Example
sentence
to
test
isspace

function

例子解釋:str中除了空格字符可以讓isspace()返回爲真,換行符也可以讓isspace()返回爲真

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章