Linux常用C函数---字符测试篇

函数讲解部分参考http://net.pku.edu.cn/~yhf/linux_c/



 

isalnum(测试字符是否为英文或数字)
相关函数
isalpha,isdigit,islower,isupper
表头文件
#include<ctype.h>
定义函数
int isalnum (int c)
函数说明
检查参数c是否为英文字母或阿拉伯数字,在标准c中相当于使用“isalpha(c) || isdigit(c)”做测试。
返回值
若参数c为字母或数字,则返回TRUE,否则返回NULL(0)。
附加说明
此为宏定义,非真正函数。
范例
/* 找出str 字符串中为英文字母或数字的字符*/
#include < ctype.h>
main()
{
char str[]=”123c@#FDsP[e?”;
int i;
for (i=0;str[i]!=0;i++ )
if ( isalnum(str[i])) printf(“%c is an alphanumeric character\n”,str[i]);
}
执行
1 is an apphabetic character
2 is an apphabetic character
3 is an apphabetic character
c is an apphabetic character
F is an apphabetic character
D is an apphabetic character
s is an apphabetic character
P is an apphabetic character
e is an apphabetic character



isalpha (测试字符是否为英文字母)
相关函数
isalnum,islower,isupper
表头文件
#include<ctype.h>
定义函数
int isalpha (int c)
函数说明
检查参数c是否为英文字母,在标准c中相当于使用“isupper(c)||islower(c)”做测试。
返回值
若参数c为英文字母,则返回TRUE,否则返回NULL(0)。
附加说明
此为宏定义,非真正函数。
范例
/* 找出str 字符串中为英文字母的字符*/
#include <ctype.h>
main()
{
char str[]=”123c@#FDsP[e?”;
int i;
for (i=0;str[i]!=0;i++)
if(isalpha(str[i])) printf(“%c is an alphanumeric character\n”,str[i]);
}
执行
c is an apphabetic character
F is an apphabetic character
D is an apphabetic character
s is an apphabetic character
P is an apphabetic character
e is an apphabetic character



isascii(测试字符是否为ASCII 码字符)
相关函数
iscntrl
表头文件
#include <ctype.h>
定义函数
int isascii(int c);
函数说明
检查参数c是否为ASCII码字符,也就是判断c的范围是否在0到127之间。
返回值
若参数c为ASCII码字符,则返回TRUE,否则返回NULL(0)。
附加说明
此为宏定义,非真正函数。
范例
/* 判断int i是否具有对映的ASCII码字符*/
#include<ctype.h>
main()
{
int i;
for(i=125;i<130;i++)
if(isascii(i))
printf("%d is an ascii character:%c\n",i,i);
else
printf("%d is not an ascii character\n",i);
}
执行
125 is an ascii character:}
126 is an ascii character:~
127 is an ascii character:
128 is not an ascii character
129 is not an ascii character



iscntrl(测试字符是否为ASCII 码的控制字符)
相关函数
isascii
表头文件
#include <ctype.h>
定义函数
int iscntrl(int c);
函数说明
检查参数c是否为ASCII控制码,也就是判断c的范围是否在0到30之间。
返回值
若参数c为ASCII控制码,则返回TRUE,否则返回NULL(0)。
附加说明
此为宏定义,非真正函数。



isdigit(测试字符是否为阿拉伯数字)
相关函数
isxdigit
表头文件
#include<ctype.h>
定义函数
int isdigit(int c)
函数说明
检查参数c是否为阿拉伯数字0到9。
返回值
若参数c为阿拉伯数字,则返回TRUE,否则返回NULL(0)。
附加说明
此为宏定义,非真正函数。
范例
/* 找出str字符串中为阿拉伯数字的字符*/
#include<ctype.h>
main()
{
char str[]="123@#FDsP[e?";
int i;
for(i=0;str[i]!=0;i++)
if(isdigit(str[i])) printf("%c is an digit character\n",str[i]);
}
执行
1 is an digit character
2 is an digit character
3 is an digit character



isgraphis(测试字符是否为可打印字符)
相关函数
isprint
表头文件
#include <ctype.h>
定义函数
int isgraph (int c)
函数说明
检查参数c是否为可打印字符,若c所对映的ASCII码可打印,且非空格字符则返回TRUE。
返回值
若参数c为可打印字符,则返回TRUE,否则返回NULL(0)。
附加说明
此为宏定义,非真正函数。
范例
/* 判断str字符串中哪些为可打印字符*/
#include<ctype.h>
main()
{
char str[]="a5 @;";
int i;
for(i=0;str[i]!=0;i++)
if(isgraph(str[i])) printf("str[%d] is printable character:%d\n",i,str[i]);
}
执行
str[0] is printable character:a
str[1] is printable character:5
str[3] is printable character:@
str[4] is printable character:;



islower(测试字符是否为小写字母)
相关函数
isalpha,isupper
表头文件
#include<ctype.h>
定义函数
int islower(int c)
函数说明
检查参数c是否为小写英文字母。
返回值
若参数c为小写英文字母,则返回TRUE,否则返回NULL(0)。
附加说明
此为宏定义,非真正函数。
范例
#include<ctype.h>
main()
{
char str[]="123@#FDsP[e?";
int i;
for(i=0;str[i]!=0;i++)
if(islower(str[i])) printf("%c is a lower-case character\n",str[i]);
}
执行
c is a lower-case character
s is a lower-case character
e is a lower-case character



isprint(测试字符是(否为可打印字符)
相关函数
isgraph
表头文件
#include<ctype.h>
定义函数
int isprint(int c);
函数说明
检查参数c是否为可打印字符,若c所对映的ASCII码可打印,其中包含空格字符,则返回TRUE。
返回值
若参数c为可打印字符,则返回TRUE,否则返回NULL(0)。
附加说明
此为宏定义,非真正函数。
范例
/* 判断str字符串中哪些为可打印字符包含空格字符*/
#include<ctype.h>
main()
{
char str[]="a5 @;";
int i;
for(i=0;str[i]!=0;i++)
if(isprint(str[i])) printf("str[%d] is printable character:%d\n",i,str[i]);
}
执行
str[0] is printable character:a
str[1] is printable character:5
str[2] is printable character:
str[3] is printable character:@
str[4] is printable character:;



isspace(测试字符是否为空格字符)
相关函数
isgraph
表头文件
#include<ctype.h>
定义函数
int isspace(int c)
函数说明
检查参数c是否为空格字符,也就是判断是否为空格('')、定位字符('\t')、CR('\r')、换行('\n')、垂直定位字符('\v')或翻页('\f')的情况。
返回值
若参数c为空格字符,则返回TRUE,否则返回NULL(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 */



ispunct(测试字符是否为标点符号或特殊符号)
相关函数
isspace,isdigit,isalpha
表头文件
#inlude<ctype.h>
定义函数
int ispunct(int c)
函数说明
检查参数c是否为标点符号或特殊符号。返回TRUE也就是代表参数c为非空格、非数字和非英文字母。
返回值
v若参数c为标点符号或特殊符号,则返回TRUE,否则返回NULL(0)。
附加说明
此为宏定义,非真正函数。
范例
/*列出字符串str中的标点符号或特殊符号*/
#include <ctype.h>
main()
{
char str[]="123c@ #FDsP[e?";
int i;
for(i=0;str[i]!=0;i++)
if(ispunct(str[i])) printf("%c\n",str[i]);
}
执行
v
@#[?



isupper(测试字符是否为大写英文字母)
相关函数
isalpha,islower
表头文件
#include<ctype.h>
定义函数
int isupper(int c)
函数说明
检查参数c是否为大写英文字母。
返回值
若参数c为大写英文字母,则返回TRUE,否则返回NULL(0)。
附加说明
此为宏定义,非真正函数。
范例
/*找出字符串str中为大写英文字母的字符*/
#include <ctype.h>
main()
{
char str[]="123c@#FDsP[e?";
int i;
for(i=0;str[i]!=0;i++)
if(isupper(str[i])) printf("%c is an uppercase character\n",str[i]);
}
执行
F is an uppercase character
D is an uppercase character
P is an uppercase character



isxdigit(测试字符是否为16进制数字)
相关函数
isalnum,isdigit
表头文件
#include<ctype.h>
定义函数
int isxdigit (int c)
函数说明
检查参数c是否为16进制数字,只要c为下列其中一个情况则返回TRUE。16进制数字:0123456789ABCDEF。
返回值
若参数c为16进制数字,则返回TRUE,否则返回NULL(0)。
附加说明
此为宏定义,非真正函数。
范例
/*找出字符串str中为十六进制数字的字符*/
#include <ctype.h>
main()
{
char str[]="123c@#FDsP[e?";
int i;
for(i=0;str[i]!=0;i++)
if(isxdigit(str[i])) printf("%c is a hexadecimal digits\n",str[i]);
}
执行
1 is a hexadecimal digits
2 is a hexadecimal digits
3 is a hexadecimal digits
c is a hexadecimal digits
F is a hexadecimal digits
D is a hexadecimal digits
e is a hexadecimal digits





Linux实例程序代码如下:

  1 #include <stdio.h>
  2 
  3 #include <ctype.h>
  4 
  5 
  6 void isalnum_fun(char *str);
  7 
  8 void isalpha_fun(char *str);
  9 
 10 void isdigit_fun(char *str);
 11 
 12 void isgraphis_fun(char *str);
 13 
 14 void islower_fun(char *str);
 15 
 16 void isprint_fun(char *str);
 17 
 18 void isspace_fun(char *str);
 19 
 20 void ispunct_fun(char *str);
 21 
 22 void isupper_fun(char *str);
 23 
 24 void isxdigit_fun(char *str);
 25 
 26 int main()
 27 
 28 {
 29     
 30     int Index;
 31     
 32     char str[40];
 33 
 34     
 35     printf("1.isalnum_fun()\n");
 36     
 37     printf("2.isalpha_fun()\n");
 38     
 39     printf("3.isdigit_fun()\n");
 40     
 41     printf("4.isgraphis_fun()\n");
 42     
 43     printf("5.islower_fun()\n");
 44     
 45     printf("6.isprint_fun()\n");
 46     
 47     printf("7.isspace_fun()\n");
 48     
 49     printf("8.ispunct_fun()\n");
 50     
 51     printf("9.isupper_fun()\n");
 52     
 53     printf("10.isxdigit_fun()\n");
 54     
 55     
 56     printf("Please input the String:\n");
 57     
 58     gets(str);
 59 
 60     
 61     while(1)
 62     
 63     {
 64         
 65         printf("Please input the operator you want done (0-exit):\n");
 66         
 67         scanf("%d",&Index);
 68         
 69         
 70         if(Index==0)
 71         
 72         {
 73             
 74             printf("Bye Bye!\n");
 75             
 76             break;
 77         
 78         }
 79         
 80         else
 81         
 82         {
 83             
 84                switch(Index)
 85             
 86               {
 87                 
 88                  case 1:isalnum_fun(str);
 89                        
 90                     break;
 91                 
 92                  case 2:isalpha_fun(str);
 93                        
 94                     break;
 95                 
 96                  case 3:isdigit_fun(str);
 97                       
 98                     break;
 99                 
100                  case 4:isgraphis_fun(str);
101                       
102                     break;
103                 
104                  case 5:islower_fun(str);
105 
106                     break;
107                 
108                  case 6:isprint_fun(str);
109                        
110                     break;
111                 
112                  case 7:isspace_fun(str);
113                       
114                     break;
115                 
116                  case 8:ispunct_fun(str);
117                        
118                     break;
119                 
120                  case 9:isupper_fun(str);
121                        
122                     break;
123                 
124                  case 10:isxdigit_fun(str);
125                        
126                     break;
127                 
128                  default:
129                         
130                 break;
131             
132             }
133         
134         }
135     
136       }
137     
138   return 0;
139 
140 }
141 
142 
143 
144 void isalnum_fun(char *str)
145 
146 {
147     
148     int i;
149     
150     for(i=0;str[i]!=0;i++);
151     
152     {
153         
154         if(isalnum(str[i]))
155             
156             printf("%c is an alphanumberic character\n",str[i]);
157     
158     }
159 
160 }
161 
162 
163 
164 void isalpha_fun(char *str)
165 
166 {
167     
168     int i;
169     
170     for(i=0;str[i]!=0;i++)
171     
172     {
173         
174         if(isalpha(str[i]))
175             
176             printf("%c is an alphanumberic character\n",str[i]);
177     
178     }
179 
180 }
181 
182 
183 
184 void isdigit_fun(char *str)
185 
186 {
187     
188     int i;
189     
190     for(i=0;str[i]!=0;i++)
191     
192     {
193         
194         if(isdigit(str[i]))
195             
196         printf("%c is an digit character\n",str[i]);
197     
198     }
199 
200 }
201 
202 
203 
204 void isgraphis_fun(char *str)
205 
206 {
207     
208     int i;
209     
210     for(i=0;str[i]!=0;i++)
211     
212     {
213         
214         if(isgraph(str[i]))
215             
216             printf("str[%d] is printable character:%c\n",i,str[i]);
217     
218     }
219 
220 }
221 
222 
223 
224 void islower_fun(char *str)
225 
226 {
227     
228     int i;
229     
230     for(i=0;str[i]!=0;i++)
231     
232     {
233         
234         if(islower(str[i]))
235             
236             printf("%c is an low_case character\n",str[i]);
237     
238     }
239 
240 }
241 
242 
243 
244 void isprint_fun(char *str)
245 
246 {
247     
248     int i;
249     
250     for(i=0;str[i]!=0;i++)
251     
252     {
253         
254         if(isprint(str[i]))
255         
256             printf("str[%d] is printable character:%c\n",i,str[i]);
257     
258     }
259     
260 
261 }
262 
263 
264 
265 void isspace_fun(char *str)
266 
267 {
268     
269     int i;
270     
271     for(i=0;str[i]!=0;i++)
272     
273     {
274 
275             if(isspace(str[i]))
276     
277                 printf("str[%d] is a white_space character:%d\n",i,str[i]);
278     
279     }    
280 
281 }
282 
283 
284 
285 void ispunct_fun(char *str)
286 
287 {
288     
289     int i;
290     
291     for(i=0;str[i]!=0;i++)
292     
293     {
294         
295         if(ispunct(str[i]))
296             
297             printf("%c\n",str[i]);
298     
299     }
300 
301 }
302 
303 
304 
305 void isupper_fun(char *str)
306 
307 {
308     
309     int i;
310     
311     for(i=0;str[i]!=0;i++)
312     
313     {
314         
315         if(isupper(str[i]))
316             
317             printf("%c is an upper_case character\n",str[i]);
318     
319     }
320 
321 }
322 
323 
324 
325 void isxdigit_fun(char *str)
326 
327 {
328     int i;
329     
330     for(i=0;str[i]!=0;i++)
331     
332     {
333         
334         if(isxdigit(str[i]))
335             
336             printf("%c is an hex digits\n",str[i]);
337 
338     }
339 
340 }

运行结果图:


posted @ 2014-12-16 21:32  vpoet  阅读(192)  评论(0编辑  收藏  举报