Interpretation and simulation implementation of important library functions in c language-commonly used character functions

Summary of commonly used character functions

The header file required by commonly used character functions is `#include<ctype.h>

Insert picture description here
Attach ASCLLC code table
Insert picture description here

Function examples and implementation

Use and implementation of int isalnum (int ch)

The function of this library function is whether it is a letter or number.
Insert picture description here
Classic case counts the number of letters and numbers in a string

#include<ctype.h>
#include<stdio.h>
#include<string.h>

int main()
{
    
    
	char ch[] = "abcd!1234";
	int sz = strlen(ch); //strlen 可以统计ch的长度
	int k = 0;
	for (int i = 0; i < sz; i++)
	{
    
    
		if (isalnum(ch[i]))  // 若ch[i]为字母或数字则为真 k++
		{
    
    
			k++;
		}
	}
	printf("%d", k);
	return 0  ;
}

achieve:

#define _CRT_SECURE_NO_WARNINGS

#include<ctype.h>
#include<stdio.h>
#include<string.h>

int My_isalNum(int const x)
{
    
    
	if (('0' <= x)&&(x <= '9') || 
	('A' <= x) &&(x<= 'Z') || 
	('a' <= x)&&(x <= 'z'))
	{
    
    
		return 1;
	}
	else 
	{
    
    
		return 0;
	}
		
}
int main()
{
    
    
	int input = 0;
	scanf("%d", &input);
	int ret=My_isalNum(input);
	if (ret == 1)
	{
    
    
		printf("yes");
	}
	else
	{
    
    
		printf("no");
	}
	return 0;
}

Use and use of int isxdigit (int ch)

The function of this library function is to judge whether it is a hexadecimal number character

int main()
{
    
    
	int i = 48;
	 
	if (isxdigit(i))
	{
    
    
		printf("yes");
	}
	else
	{
    
    
		printf("no");
	}
	return 0;
}

Implementation: The implementation is the same as isalnum, only the case of 1-9 ae needs to be guaranteed.

Case conversion and judgment

Case determination

Function and realization of int islower(int ch)
#include<ctype.h>
#include<stdio.h>
#include<string.h>

int main()
{
    
    
	int i = 48; //i属于[97,122] 为真 返回1

	if (islower(i))
	{
    
    
		printf("yes");
	}
	else
	{
    
    
		printf("no");
	}
	return 0;
}

achieve:

#include<ctype.h>
#include<stdio.h>
#include<string.h>

int My_islower(int x)
{
    
    

	if ((x >= 'a') && (x <= 'z'))
	{
    
    
		return 1;
	}
	else
	{
    
    
		return 0 ;
	}
}
int main()
{
    
    
	int i = 48; //i属于[97,122] 为真 返回1

	if (My_islower(i))
	{
    
    
		printf("yes");
	}
	else
	{
    
    
		printf("no");
	}
	return 0;
}

Case conversion

Convert lowercase to uppercase: int toupper(int ch)

#include<ctype.h>
#include<stdio.h>
#include<string.h>


int main()
{
    
    
	int i = 112; // 小写字符p
	int ret= toupper(i);
	printf("%c", ret);
	return 0;
}

Self-actualization


#include<ctype.h>
#include<stdio.h>
#include<string.h>

int My__touPper(int x)
{
    
    
	if((x>='a')&&(x<='z'))
	{
    
    
		return  x - 32;
	}
	else
	{
    
    
		return 0;
	}
}


int main()
{
    
    
	int i = 112;
	int ret= My__touPper(i); // 是小写则返回大写 否则返回0
	printf("%c", ret);
	return 0;
}

Note:

  1. Capitalization only needs to change the conditions.

Guess you like

Origin blog.csdn.net/qq_45849625/article/details/114760375