字符函数&内存函数———C语言

字符分类函数

头文件:

        ctype.h

函数功能
iscntrl判断字符是否为控制字符
isspace判断字符是否为空白字符(空格,换页、换行、回车、制表符或垂直制表符)
isdigit判断字符是否为十进制数字
isxdigit判断字符是否为十六进制数字(0-9)(a-f)(A-F)
isupper判断字符是否为大写英文字母
islower判断字符是否为小写英文字母
isalpha判断字符是否为英文字母
isalnum判断字符是否为字母或数字
ispunct判断字符是否为标点符号

字符转换函数:

函数功能
tolower把大写字母转换为小写字母
toupper把小写字母转换为大写字母
#include <stdio.h>
#include <ctype.h>
int main() {
    char ch1, ch2;
    printf("小写转大写:\n");
    printf("input a character:");
    scanf("%c", &ch1);
    ch2 = toupper(ch1);
    printf("transform %c to %c.\n", ch1, ch2);
    printf("大写转小写:\n");
    char str[1024] = "Hello World!";
    char* p_str = str;
    char res[1024] = { 0 };
    char* p_res = res;
    while (*p_str) {
        if (isupper(*p_str)) {
            *p_res = tolower(*p_str);
        }
        else {
            *p_res = *p_str;
        }
        p_str++;
        p_res++;
    }    
    printf("%s transform:%s\n", str, res);
    return 0;
}

内存函数:

1.memcpy

原型:void * memcpy ( void * destination, const void * source, size_t num );

原理:从source开始位置向后复制num个字节的数据到destination的内存位置

#include <stdio.h>
#include <string.h>
typedef  struct  Stu{
        char name[1024];
        int age;
    }S;
int main() {
        int nums[] = { 1,2,3,4 };
        int numsBak[10] = { 0 };
        memcpy(numsBak, nums, sizeof(nums));
        int length = sizeof(nums) / sizeof(nums[0]);
        for (int i = 0; i < length; i++) {
                printf("%d\n", numsBak[i]);
        }        
        // 结构体的拷贝
        S students[] = { {"xiaoming",10},{"xiaozhao",30} };
        S studentsBak[3] = { 0 };
        memcpy(studentsBak, students, sizeof(students));
        for (int i = 0; i < 3; i++) {
                S student = studentsBak[i];
                printf("uname:%s,age:%d\n", student.name, student.age);
        }
        return 0;
}

注意:该函数不检测源中的任何终止字符,它总是精确地复制num个字符 

eg:

#include <stdio.h>
#include <string.h>
int main() {
    char src[] = "He was an unusually\0 complex man";//这里的/0不被在意
    char dist[1024] = { 0 };    memcpy(dist, src, 200);
    printf("src:%s\ndist:%s\n", src, dist);
    printf("dist[21]=%c\n",dist[21]);
    return 0;
}

2.memmove

原型:void * memmove ( void * destination, const void * source, size_t num );

原理:将num个字节的值从源指向的位置复制到目标指向的内存块。复制就像使用了中间缓存,从而允许目标和源重叠, 该函数不检测源中的任何终止字符,它总是精确地复制num个字符。

section one :

        destination的起始地址在src起始地址之后

section two :

        destination的结束地址在src结束地址之前

#include <stdio.h>
#include <string.h>
int main() {
    char str[] = "abcdef";
    // 重叠的区域复制:从 str + 1 开始的部分复制到 str 开头
    memmove(str, str+1, 5);
    printf("Result: %s\n", str);  // 输出: "bcdef"
    return 0;
}

3.memcmp

原型:int memcmp ( const void * ptr1, const void * ptr2, size_t num );

原理:

  • 将ptr1所指向的内存块的前num字节与ptr2所指向的前num字节进行比较,如果它们都匹配则返回0
  • 在两个内存块中不匹配的第一个字节在ptr1中的值小于ptr2中的值,则返回<0的数(比较的是字母转换后的ASCII值)
  • 在两个内存块中不匹配的第一个字节在ptr1中的值大于ptr2中的值,则返回>0的数
#include <stdio.h>
#include <string.h>
int main(){
    char buffer1[] = "DWGaOtP12df0";
    char buffer2[] = "DWgAOTP12DF0";
    int n = memcmp(buffer1, buffer2, sizeof(buffer1));
    if (n > 0) {
        printf("'%s' is greater than '%s'.\n", buffer1, buffer2);
    }    else if (n < 0) {
        printf("'%s' is less than '%s'.\n", buffer1, buffer2);
    }    else { 
        printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
    }
    return 0;
}

4.memset

原型:void * memset ( void * ptr, int value, size_t num );

原理:将ptr指向的内存块的前num个字节设置为指定的值(解释为unsigned char)

#include <stdio.h>
#include <string.h>
int main() {
    char name[] = "almost every programmer should know memset!";
    // 将name指向的内存块的前6个字节设置为-
    memset(name, '-', 6);
    printf("%s\n", name);
    return 0;
}

 

  • 这是本人的学习笔记不是获利的工具,小作者会一直写下去,希望大家能多多监督
  • 文章会每攒够两篇进行更新发布(受平台原因,也是希望能让更多的人看见)
  • 感谢各位的阅读希望我的文章会对诸君有所帮助
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值