用C說話斷定字符能否為空白字符或特別字符的辦法。本站提示廣大學習愛好者:(用C說話斷定字符能否為空白字符或特別字符的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是用C說話斷定字符能否為空白字符或特別字符的辦法正文
C說話isspace()函數:斷定字符能否為空白字符
頭文件:
#include <ctype.h>
界說函數:
int isspace(int c);
函數解釋:檢討參數c能否為空格字符,也就是斷定能否為空格(' ')、定位字符(' \t ')、CR(' \r ')、換行(' \n ')、垂直定位字符(' \v ')或翻頁(' \f ')的情形。
前往值:若參數c 為空白字符,則前往非 0,不然前往 0。
附加解釋:此為宏界說,非真正函數。
典范:將字符串str[]中內含的空格字符找出,並顯示空格字符的ASCII 碼。
#include <ctype.h>
main(){
char str[] = "123c @# FD\tsP[e?\n";
int i;
for(i = 0; str[i] != 0; i++)
if(isspace(str[i]))
printf("str[%d] is a white-space character:%d\n", i, str[i]);
}
履行成果:
str[4] is a white-space character:32 str[7] is a white-space character:32 str[10] is a white-space character:9 // \t str[16] is a white-space character:10 // \t
C說話ispunct()函數:斷定字符能否為標點符號或特別字符
頭文件:
#inlude <ctype.h>
ispunct() 函數用來檢測一個字符能否為標點符號或特別字符,其原型為:
int ispunct(int c);
【參數】c 為須要檢測的字符。
【前往值】若 c 為標點符號或特別符號(非空格、非數字和非英文字母)前往非 0 值,不然前往 0。
留意,此為宏界說,非真正函數。
【實例】列出字符串str 中的標點符號或特別符號。
#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;
}
輸入成果:
Sentence contains 2 punctuation characters.