c言語での重要なライブラリ関数の解釈とシミュレーションの実装-一般的に使用される文字関数

一般的に使用される文字関数の要約

一般的に使用される文字関数に必要なヘッダーファイルは `#include <ctype.h>です。

ここに画像の説明を挿入します
ASCLLCコードテーブルを添付する
ここに画像の説明を挿入します

関数の例と実装

int isalnum(int ch)の使用と実装

このライブラリ関数の機能は、文字か数字かです。
ここに画像の説明を挿入します
古典的なケースでは、文字列内の文字と数字の数をカウントします。

#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  ;
}

成し遂げる:

#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;
}

int isxdigit(int ch)の使用と使用

このライブラリ関数の機能は、それは16進数のであるかどうかを裁判官にある文字

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

実装:実装はisalnumと同じですが、保証する必要があるのは1〜9aeの場合のみです。

ケースの変換と判断

ケースの決定

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;
}

成し遂げる:

#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;
}

ケース変換

小文字を大文字に変換します: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;
}

自己実現


#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;
}

注意:

  1. 大文字化は条件を変更するだけです。

おすすめ

転載: blog.csdn.net/qq_45849625/article/details/114760375