C语言:字符和字符串库函数

任务代码:

统计数字字符的个数isdigit || isalpha

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

int main()
{
    char str[50];
    printf("请输入字符串:");
    gets(str);
    
    int i=0,n=0;
    while (str[i]!='\0')
    {
        if(isdigit(str[i]))
        {
            n++;
        }
        i++;
    }
    printf("数字字符有:%d个!\n",n);
    return 0;
}

两个字符串连结 strcat

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

int main()
{
    char str1[50]="i love you -";
    char str2[50]=" my dear";
    printf("%s\n",strcat(str1, str2));
    return 0;
}

字符串复制函数 strcpy

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

int main()
{
    char str1[50];
    char str2[50]=" my dear";
    strcpy(str1, str2);
    printf("%s\n",str1);
    return 0;
}


字符串比较函数 strcmp:


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

int main()
{
    char str1[50]="my dear";
    char str2[50]=" my dear";
    if(strcmp(str1, str2)!=0)
    {
        printf("两个字符串不同!");
    }
    else printf("字符串相同!");
    return 0;
}

字符串长度函数strlen:

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

int main()
{
    
    char str[]="beijing";
    printf("有%ld个字符!\n4",strlen(str));//注意这里strlen 返回的是size_t的长度,是一个系统定义的宏,是一个long型的长度
    return 0;
    
}


执行情况:

第一题:


第二题:



第三题:


第四题:


第五题:



知识总结:

字符函数&字符串函数




字符函数:



字符串函数:



两个字符串的连结:




字符串的复制:



字符串比较:



字符串长度函数:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值