在實(shí)際編寫程序的過程中妻怎,為了加強(qiáng)程序的穩(wěn)健性,針對(duì)一串?dāng)?shù)據(jù)搁痛,我們常常需要進(jìn)行預(yù)先的處理长搀,然后才會(huì)真正的進(jìn)行數(shù)據(jù)處理。比如我們首先要判斷數(shù)據(jù)是否是數(shù)字鸡典、是否是字母源请、是否是ASCII碼、是否是空格等等彻况。下面就介紹一下這些函數(shù)谁尸,及其用法。附錄中有C語言函數(shù)速查手冊(cè)纽甘,便于閱讀者使用良蛮。
一、字符數(shù)據(jù)識(shí)別
1.1 字母數(shù)字判斷
【函數(shù)原型】:
#include <ctype.h>
int isalnum(int c);
【函數(shù)說明】:
判斷字符c是否為字母或數(shù)字悍赢。當(dāng)c為數(shù)字09或字母az及A~Z時(shí)决瞳,返回非零值货徙,否則返回零。
【使用實(shí)例】:
#include <ctype.h>
#include <syslib.h>
int main(void)
{
int c;
clrscr(); // clear screen
c='a';
printf("%c:%s\n",c,isalnum(c)?"yes":"no");
c='7';
printf("%c:%s\n",c,isalnum(c)?"yes":"no");
c='@';
printf("%c:%s\n",c,isalnum(c)?"yes":"no");
getchar();
return 0;
}
1.2 英文字母判斷
【函數(shù)原型】:
#include <ctype.h>
int isalpha(int c);
【函數(shù)說明】:
判斷字符c是否為英文字母皮胡。當(dāng)c為英文字母az或AZ時(shí)痴颊,返回非零值,否則返回零胸囱。
【使用實(shí)例】:
#include <ctype.h>
#include <stdio.h>
#include <syslib.h>
int main(void)
{
int c;
clrscr(); // clear screen
printf("Press a key");
for(;;)
{
c=getchar();
clrscr();
printf("%c: %s letter",c,isalpha(c)?"is":"not");
}
return 0; // just to avoid warnings by compiler
}
1.3 小寫字母判斷
【函數(shù)原型】:
#include <ctype.h>
int islower(int c);
【函數(shù)說明】:
判斷字符c是否為小寫英文字母祷舀。當(dāng)c為小寫英文字母a~z時(shí),返回非零值烹笔,否則返回零裳扯。
【使用實(shí)例】:
#include <ctype.h>
#include <syslib.h>
int main(void)
{
int c;
clrscr(); // clear screen
c='a';
printf("%c:%s\n",c,islower(c)?"yes":"no");
c='A';
printf("%c:%s\n",c,islower(c)?"yes":"no");
c='7';
printf("%c:%s\n",c,islower(c)?"yes":"no");
getchar();
return 0;
}
1.4 大寫字母判斷
【函數(shù)原型】:
#include <ctype.h>
int isupper(int c);
【函數(shù)說明】:
判斷字符c是否為大寫英文字母。當(dāng)c為大寫英文字母A~Z時(shí)谤职,返回非零值饰豺,否則返回零。
【使用實(shí)例】:
#include <ctype.h>
#include <syslib.h>
int main(void)
{
int c;
clrscr(); // clear screen
c='a';
printf("%c:%s\n",c,isupper(c)?"yes":"no");
c='A';
printf("%c:%s\n",c,isupper(c)?"yes":"no");
c='7';
printf("%c:%s\n",c,isupper(c)?"yes":"no");
getchar();
return 0;
}
1.5 阿拉伯?dāng)?shù)字判斷
【函數(shù)原型】:
#include <ctype.h>
int isdigit(int c);
【函數(shù)說明】:
判斷字符c是否為數(shù)字允蜈。當(dāng)c為數(shù)字0~9時(shí)冤吨,返回非零值,否則返回零饶套。
【使用實(shí)例】:
#include <ctype.h>
#include <syslib.h>
int main(void)
{
int c;
clrscr(); // clear screen
c='a';
printf("%c:%s\n",c,isdigit(c)?"yes":"no");
c='9';
printf("%c:%s\n",c,isdigit(c)?"yes":"no");
c='*';
printf("%c:%s\n",c,isdigit(c)?"yes":"no");
getchar();
return 0;
}
1.6 十六進(jìn)制判斷
【函數(shù)原型】:
#include <ctype.h>
int isxdigit(int c);
【函數(shù)說明】:
判斷字符c是否為十六進(jìn)制數(shù)字漩蟆。當(dāng)c為數(shù)字09或?yàn)閍f(或A~F)之間的十六進(jìn)制數(shù)字時(shí),返回非零值妓蛮,否則返回零怠李。
【使用實(shí)例】:
#include <ctype.h>
#include <syslib.h>
int main(void)
{
int c;
clrscr(); // clear screen
c='a';
printf("%c:%s\n",c,isxdigit(c)?"yes":"no");
c='9';
printf("%c:%s\n",c,isxdigit(c)?"yes":"no");
c='*';
printf("%c:%s\n",c,isxdigit(c)?"yes":"no");
getchar();
return 0;
}
1.7 ASCII判斷
【函數(shù)原型】:
#include <ctype.h>
int isascii(int c);
【函數(shù)說明】:
判斷字符c是否為ASCII碼。當(dāng)c為ASCII碼時(shí)蛤克,返回非零值捺癞,否則返回零。ASCII碼指0x00~0x7F之間的字符构挤。
【使用實(shí)例】:
#include <ctype.h>
#include <syslib.h>
int main(void)
{
char s[]="文曲星-BJGGV";
int i=12; // length of string s
clrscr(); // clear screen
textmode(0xE0); // make sure LCD mode is 3 big line
printf("%s\n",s);
for(i=0;i<12;i++)
{
if(isascii(s[i]))
putchar('^');
else
putchar('.');
}
getchar();
return 0;
}
1.8 空字符判斷(TAB或空格)
【函數(shù)原型】:
#include <ctype.h>
int isblank (int c);
【函數(shù)說明】:
判斷字符c是否為TAB或空格髓介。當(dāng)c為TAB或空格時(shí),返回非零值筋现,否則返回零唐础。
【使用實(shí)例】:
#include <ctype.h>
#include <syslib.h>
int main(void)
{
int c;
clrscr(); // clear screen
c='a';
printf("%c:%s\n",c,isalnum(c)?"yes":"no");
c='7';
printf("%c:%s\n",c,isalnum(c)?"yes":"no");
c='@';
printf("%c:%s\n",c,isalnum(c)?"yes":"no");
}
1.9 空白字符判斷
【函數(shù)原型】:
#include <ctype.h>
int isspace(int c);
【函數(shù)說明】:
判斷字符c是否為空白符。當(dāng)c為空白符時(shí)矾飞,返回非零值彻犁,否則返回零』舜龋空白符指空格、水平制表符驼鹅、垂直制表符微谓、換頁森篷、換行和回車。
【使用實(shí)例】:
#include <ctype.h>
#include <syslib.h>
int main(void)
{
char s[]="Test Line 1\tend\nTest Line 2\r";
int i;
clrscr(); // clear screen
for(i=0;i<strlen(s);i++)
{
if(isspace(s[i]))
putchar('.');
else
putchar(s[i]);
}
getchar();
return 0;
}
1.10 標(biāo)點(diǎn)符號(hào)判斷
【函數(shù)原型】:
#include <ctype.h>
int ispunct(int c);
【函數(shù)說明】:
判斷字符c是否為標(biāo)點(diǎn)符號(hào)豺型。當(dāng)c為標(biāo)點(diǎn)符號(hào)時(shí)仲智,返回非零值,否則返回零姻氨。標(biāo)點(diǎn)符號(hào)指那些既不是字母數(shù)字钓辆,也不是空格的可打印字符。
【使用實(shí)例】:
#include <ctype.h>
#include <syslib.h>
#include <string.h>
int main(void)
{
char s[]="Hello, Rain!";
int i;
clrscr(); // clear screen
printf("%s\n",s);
for(i=0;i<strlen(s);i++)
{
if(ispunct(s[i]))
printf("^");
else
printf(".");
}
getchar();
return 0;
}
1.11 可打印字符判斷(除空格)
【函數(shù)原型】:
#include <ctype.h>
int isgraph(int c);
【函數(shù)說明】:
判斷字符c是否為除空格外的可打印字符肴焊。當(dāng)c為可打印字符(0x21~0x7E)時(shí)前联,返回非零值,否則返回零娶眷。
【使用實(shí)例】:
#include <ctype.h>
#include <syslib.h>
int main(void)
{
int c;
clrscr(); // clear screen
c='a';
printf("%c:%s\n",c,isgraph(c)?"yes":"no");
c=' '; // 0x20
printf("%c:%s\n",c,isgraph(c)?"yes":"no");
c=0x7f;
printf("%c:%s\n",c,isgraph(c)?"yes":"no");
getchar();
return 0;
}
1.12 可打印字符判斷(含空格)
【函數(shù)原型】:
#include <ctype.h>
int isprint(int c);
【函數(shù)說明】:
判斷字符c是否為可打印字符(含空格)似嗤。當(dāng)c為可打印字符(0x20~0x7E)時(shí),返回非零值届宠,否則返回零烁落。
【使用實(shí)例】:
#include <ctype.h>
#include <syslib.h>
int main(void)
{
int c;
clrscr(); // clear screen
c='a';
printf("%c:%s\n",c,isprint(c)?"yes":"no");
c=' ';
printf("%c:%s\n",c,isprint(c)?"yes":"no");
c=0x7f;
printf("%c:%s\n",c,isprint(c)?"yes":"no");
getchar();
return 0;
}
1.13 控制字符判斷
【函數(shù)原型】:
#include <ctype.h>
int iscntrl(int c);
【函數(shù)說明】:
該函數(shù)判斷字符c是否為控制字符。當(dāng)c在0x00~0x1F之間或者等于0x7F(DEL)時(shí)豌注,返回非零值伤塌,否則返回零。
【使用實(shí)例】:
#include <ctype.h>
#include <syslib.h>
int main(void)
{
int c;
clrscr(); // clear screen
c='a';
printf("%x:%s\n",c,iscntrl(c)?"yes":"no");
c=0x0d;
printf("%x:%s\n",c,iscntrl(c)?"yes":"no");
c=0x7f;
printf("%x:%s\n",c,iscntrl(c)?"yes":"no");
getchar();
return 0;
}
二轧铁、字符數(shù)據(jù)轉(zhuǎn)換
2.1 字符轉(zhuǎn)換為ASCII碼
【函數(shù)原型】:
#include <ctype.h>
int toascii(int c);
【函數(shù)說明】:
該函數(shù)將字符c轉(zhuǎn)換為ASCII碼每聪。該函數(shù)將字符c的高位清零,僅保留低七位属桦,返回轉(zhuǎn)換后的數(shù)值熊痴。
【使用實(shí)例】:
#include <ctype.h>
#include <syslib.h>
int main(void)
{
char s[]="文曲星-BJGGV";
int i=12; // length of string s
clrscr(); // clear screen
textmode(0xE0); // make sure LCD mode is 3 big line
printf("%s\n",s);
for(i=0;i<12;i++)
putchar(toascii(s[i]));
getchar();
return 0;
}
2.2 大小寫之間轉(zhuǎn)換
【函數(shù)原型】:
#include <ctype.h>
int toupper(int c);
int tolower(int c);
【函數(shù)說明】:
- toupper()函數(shù)將字符c轉(zhuǎn)換為大寫英文字母。如果c為小寫英文字母聂宾,則返回對(duì)應(yīng)的大寫字母果善;否則返回c原來的值;
- tolower()函數(shù)將字符c轉(zhuǎn)換為小寫英文字母系谐。如果c為大寫英文字母巾陕,則返回對(duì)應(yīng)的小寫字母;否則返回c原來的值纪他。
【使用實(shí)例1】:
#include <ctype.h>
#include <syslib.h>
int main(void)
{
char *s="Hello, World!";
int i;
clrscr(); // clear screen
printf("%s\n",s);
for(i=0;i<strlen(s);i++)
{
putchar(toupper(s[i]));
}
getchar();
return 0;
}
【使用實(shí)例2】:
#include <ctype.h>
#include <syslib.h>
int main(void)
{
char *s="Hello, World!";
int i;
clrscr(); // clear screen
printf("%s\n",s);
for(i=0;i<strlen(s);i++)
{
putchar(tolower(s[i]));
}
getchar();
return 0;
}
2.3 整數(shù)與字符串間轉(zhuǎn)換
【函數(shù)原型】:
#include <stdlib.h>
char *itoa(int i);
int atoi(const char *nptr);
【函數(shù)說明】:
- itoa()函數(shù)把整數(shù)i轉(zhuǎn)換成字符串鄙煤,并返回指向轉(zhuǎn)換后的字符串的指針;
- atoi()函數(shù)把整數(shù)字符串nptr轉(zhuǎn)換成整形數(shù)據(jù)并返回茶袒;atoi()會(huì)掃描參數(shù)nptr字符串梯刚,跳過前面的空格字符,直到遇上數(shù)字或正負(fù)符號(hào)才開始做轉(zhuǎn)換薪寓,而再遇到非數(shù)字或字符串結(jié)束符(‘\0’)時(shí)結(jié)束轉(zhuǎn)換亡资,并將結(jié)果返回澜共;atoi()相當(dāng)于函數(shù)strtol(nptr, (char **)NULL, 10);只是atoi()不檢測(cè)錯(cuò)誤锥腻。
【使用實(shí)例1】:
#include <syslib.h>
#include <stdlib.h>
int main(void)
{
int i=7412;
clrscr(); // clear screen
textmode(0x00);
printf("%d",i);
printf("%s",itoa(i));
getchar();
return 0;
}
【使用實(shí)例2】:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
char *str = "12345.67";
n = atoi(str);
printf("string = %s integer = %d\n", str, n);
return 0;
}
2.4 長整型與字符串間轉(zhuǎn)換
【函數(shù)原型】:
#include <stdlib.h>
long atol(const char *nptr);
long long atoll(const char *nptr);
long long atoq(const char *nptr);
long int strtol(const char *nptr, char **endptr, int base);
long long int strtoll(const char *nptr, char **endptr, int base);
【函數(shù)說明】:
- atoll()函數(shù)把長整數(shù)字符串nptr轉(zhuǎn)換成長整形數(shù)據(jù)并返回嗦董;
- atoll()函數(shù)把長整型數(shù)據(jù)字符串轉(zhuǎn)換為long long類型數(shù)據(jù)并返回;
- atoq()函數(shù)與atoll()函數(shù)相同瘦黑,把一個(gè)長整型數(shù)據(jù)字符串轉(zhuǎn)換為long long類型數(shù)據(jù)并返回京革;atoq()是atoll()的過時(shí)名;
- strtol()函數(shù)將參數(shù)nptr字符串根據(jù)參數(shù)base來轉(zhuǎn)換成長整型數(shù)幸斥;參數(shù)base代表采用的進(jìn)制方式匹摇,范圍從2至36,或者0睡毒;當(dāng)base值為0時(shí)則采用十進(jìn)制做轉(zhuǎn)換来惧,但遇到如‘0x’前置字符則使用十六進(jìn)制做轉(zhuǎn)換;一開始strtol()函數(shù)會(huì)掃描參數(shù)nptr字符串演顾,跳過前面的空格字符供搀,直到遇上數(shù)字或者正負(fù)符號(hào)才開始做轉(zhuǎn)換,再遇到非數(shù)字或字符串結(jié)束符(‘\0’)結(jié)束轉(zhuǎn)換钠至,并將結(jié)果返回葛虐;若endptr不為NULL,則會(huì)將遇到不合條件而終止的nptr中的字符指針由endptr返回棉钧;strtol()返回轉(zhuǎn)換后的長整型數(shù)據(jù)屿脐,否則返回ERANGE并將錯(cuò)誤代碼存入errno中(ERANGE表示指定的轉(zhuǎn)換字符串超出合法范圍)。
【使用實(shí)例1】:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
long l;
char *str = "98765432";
l = atol(lstr);
printf("string = %s integer = %ld\n", str, l);
return(0);
}
【使用實(shí)例2】:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, char *argv[])
{
int base;
char *endptr, *str;
long val;
if (argc < 2) {
fprintf(stderr, "Usage: %s str [base]\n", argv[0]);
exit(EXIT_FAILURE);
}
str = argv[1];
base = (argc > 2) ? atoi(argv[2]) : 10;
errno = 0; /* To distinguish success/failure after call */
val = strtol(str, &endptr, base);
/* Check for various possible errors */
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
|| (errno != 0 && val == 0)) {
perror("strtol");
exit(EXIT_FAILURE);
}
if (endptr == str) {
fprintf(stderr, "No digits were found\n");
exit(EXIT_FAILURE);
}
/* If we got here, strtol() successfully parsed a number */
printf("strtol() returned %ld\n", val);
if (*endptr != ’\0’) /* Not necessarily an error... */
printf("Further characters after number: %s\n", endptr);
exit(EXIT_SUCCESS);
}
2.5 無符號(hào)長整型與字符串間轉(zhuǎn)換
【函數(shù)原型】:
#include <stdlib.h>
unsigned long int strtoul(const char *nptr, char **endptr, int base);
unsigned long long int strtoull(const char *nptr, char **endptr, int base);
【函數(shù)說明】:
- strtoul()函數(shù)將參數(shù)nptr字符串根據(jù)參數(shù)base來轉(zhuǎn)換成無符號(hào)長整型數(shù)宪卿;參數(shù)base代表采用的進(jìn)制方式的诵,范圍從2至36,或者0佑钾;當(dāng)base值為0時(shí)則采用十進(jìn)制做轉(zhuǎn)換西疤,但遇到如‘0x’前置字符則使用十六進(jìn)制做轉(zhuǎn)換;一開始strtoul()函數(shù)會(huì)掃描參數(shù)nptr字符串休溶,跳過前面的空格字符代赁,直到遇上數(shù)字才開始做轉(zhuǎn)換,再遇到非數(shù)字或字符串結(jié)束符(‘\0’)結(jié)束轉(zhuǎn)換兽掰,并將結(jié)果返回芭碍;若endptr不為NULL,則會(huì)將遇到不合條件而終止的nptr中的字符指針由endptr返回孽尽;
- strtoull()函數(shù)與strtoul()函數(shù)類似窖壕,只是將字符串轉(zhuǎn)換為unsigned long long類型的數(shù)據(jù)。
2.6 浮點(diǎn)型數(shù)據(jù)與字符串間轉(zhuǎn)換
【函數(shù)原型】:
#include <stdlib.h>
double atof(const char *nptr);
double strtod(const char *nptr, char **endptr);
float strtof(const char *nptr, char **endptr);
long double strtold(const char *nptr, char **endptr);
char *gcvt(double number, size_t ndigit, char *buf);
char *ecvt(double number, int ndigits, int *decpt, int *sign);
char *fcvt(double number, int ndigits, int *decpt, int *sign);
【函數(shù)說明】:
- atof()函數(shù)將一個(gè)浮點(diǎn)數(shù)字符串轉(zhuǎn)化為一個(gè)浮點(diǎn)型數(shù)據(jù)并返回。其運(yùn)行結(jié)果相當(dāng)于:strtod()函數(shù)艇拍,但atof()并不對(duì)檢測(cè)錯(cuò)誤類型狐蜕;
- strtod()函數(shù)將一個(gè)浮點(diǎn)數(shù)字符串轉(zhuǎn)化為一個(gè)double型數(shù)據(jù)并返回;strtod()會(huì)掃描nptr字符串卸夕,跳過前面的空格字符,直到遇上數(shù)字或正負(fù)符號(hào)才開始做轉(zhuǎn)換婆瓜,到出現(xiàn)非數(shù)字或字符串結(jié)束符(‘\0’)才結(jié)束轉(zhuǎn)換快集,并將結(jié)果返回;若endptr不為NULL廉白,則會(huì)將遇到不合格條件而終止的nptr中的字符串指針由endptr傳回个初;參數(shù)nptr字符串可包含正負(fù)號(hào)、小數(shù)點(diǎn)或E(e)來表示指數(shù)部分猴蹂,如123.456或123e-2院溺;
- strtof()函數(shù)將一個(gè)單精度浮點(diǎn)數(shù)字符串轉(zhuǎn)化為一個(gè)單精度浮點(diǎn)型數(shù)據(jù)并返回;
- strtold()函數(shù)將一個(gè)長雙精度型字符串轉(zhuǎn)化為一個(gè)長雙精度浮點(diǎn)型數(shù)據(jù)并返回磅轻;
- gcvt()函數(shù)將浮點(diǎn)數(shù)number轉(zhuǎn)換為長度為ndigit的字符串珍逸,使用FORTRANF或printfE格式;number是待轉(zhuǎn)換的值聋溜;ndigit是轉(zhuǎn)換后有效數(shù)字的個(gè)數(shù)谆膳;buf指向轉(zhuǎn)換后字符串存放的位置;
- ecvt()和fcvt()函數(shù)將浮點(diǎn)數(shù)值轉(zhuǎn)換為字符串撮躁,低位數(shù)字四舍五入漱病,數(shù)字舍入到指定位;number是待轉(zhuǎn)換的值把曼;ndigit是轉(zhuǎn)換后有效數(shù)字的個(gè)數(shù)杨帽;decpt指小數(shù)點(diǎn)的位置;sign是數(shù)值符號(hào)嗤军;函數(shù)返回轉(zhuǎn)換后的字符串指針注盈。
【使用實(shí)例1】:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
float f;
char *str = "12345.67";
f = atof(str);
printf("string = %s float = %f\n", str, f);
return 0;
}
【使用實(shí)例2】:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char input[80], *endptr;
double value;
printf("Enter a floating point number:");
gets(input);
value = strtod(input, &endptr);
printf("The string is %s the number is %lf\n", input, value);
return 0;
}
【使用實(shí)例3】:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[25];
double num;
int sig=5;
num=9.876;
gcvt(num,sig,str);
printf( "string=%s ",str);
num=-123.4567;
gcvt(num,sig,str);
printf( "string=%s ",str);
num=0.678e5;
gcvt(num,sig,str);
printf( "string=%s ",str);
return(0);
}
【使用實(shí)例4】:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
char *string;
double value;
int dec,sign;
int ndig=10;
clrscr();
value=9.876;
string=ecvt(value,ndig,&dec,&sign);
printf( "string=%s dec=%d sign=%d ",string,dec,sign);
value=-123.45;
ndig= 15;
string=ecvt(value,ndig,&dec,&sign);
printf( "string=%s dec=%d sign=%d ",string,dec,sign);
value=0.6789e5;
ndig=5;
string=ecvt(value,ndig,&dec,&sign);
printf( "string=%s dec=%d sign=%d ",string,dec,sign);
return 0;
}