Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use isalnum () and iscntrl () in C language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the C language isalnum () and iscntrl () how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that you read this C language isalnum () and iscntrl () how to use the article will have a harvest, let's take a look.

The isalnum () function is used to check whether the characters passed are letters or decimal digits. Its function prototype is as follows:

_ CRTIMP int _ _ cdecl isalnum (int _ C)

A return value of non-zero (true) indicates that the parameter c is a letter or decimal number, and a return value of zero (false) indicates that the parameter c is neither a decimal number nor a letter.

Here is a simple example to demonstrate its usage.

# include # include int main () {int var1 = 'alphanumeric; int var2 =' 8#; int var3 ='\ tasking; int var4 =''; if (isalnum (var1)) {printf ("var1 = |% c | alphanumeric\ n", var1);} else {printf ("var1 = |% c | not alphanumeric\ n", var1) } if (isalnum (var2)) {printf ("var2 = |% c | alphanumeric\ n", var2);} else {printf ("var2 = |% c | not alphanumeric\ n", var2);} if (isalnum (var3)) {printf ("var3 = |% c | alphanumeric\ n", var3) } else {printf ("var3 = |% c | not alphanumeric\ n", var3);} if (isalnum (var4)) {printf ("var4 = |% c | alphanumeric\ n", var4);} else {printf ("var4 = |% c | not alphanumeric\ n", var4);} return (0);}

Define four variables, a letter, a number, a tab, and an empty character. The output is as follows:

The return value of the first two variables is true and the return value of the last two variables is false.

The isalnum () function can detect letters or numbers at the same time. If you want to detect letters or numbers separately, the library function also provides special detection functions isalpha and isdigit. Their function prototypes are as follows:

_ CRTIMP int _ _ cdecl isalpha (int _ C); _ CRTIMP int _ _ cdecl isdigit (int _ C)

Replace the functions in the above code with these two functions.

# include # include int main () {int var1 ='a letter; int var2 ='8 letters; if (isalpha (var1)) {printf ("var1 = |% c | is the letter\ n", var1);} else {printf ("var1 = |% c | not the letter\ n", var1);} if (isdigit (var2)) {printf ("var2 = |% c | is a number\ n", var2) } else {printf ("var2 = |% c | not a number\ n", var2);} return (0);}

The running results are as follows:

The C language standard library also provides the control character detection function iscntrl.

The so-called "control characters" refer to those special characters that have some special function, will not be displayed on the screen, and will not occupy the position of the characters. In the ASCII code table, the first 32 characters are control characters.

The prototype of the control character detection function iscntrl is as follows:

_ CRTIMP int _ _ cdecl iscntrl (int _ C)

If c is a control character, the function returns a non-zero value, otherwise it returns 0.

Let's demonstrate its usage with a simple piece of code.

# include # include int main () {int I = 0, j = 0; char str1 [] = "abc\ a def\ t gh"; char str2 [] = "123\ n 456"; / * output the string until the control character\ a * / while (! iscntrl (str1 [I]) {putchar (str1 [I])) } / * output the string until the control character\ n * / while (! iscntrl (str2 [j])) {putchar (str2 [j]); return (0);}

Define two strings with control characters in the middle, and then detect whether the character is a control character when printing the string, and end printing if it is a control character. The output is as follows:

With the output, you can see that only the characters in front of the control character are printed.

As opposed to control characters are printable characters, which are "ordinary" characters that will be displayed on the screen and occupy character positions. The characters encoded 32-127 in the ASCII code table are printable characters. Printable characters are frequently used numbers, letters, symbols, and other characters that can be seen on the screen.

This is the end of the article on "how to use isalnum () and iscntrl () in C language". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use isalnum () and iscntrl () in C language". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report