在C语言中 string.h中常见的4个函数(基础版)

1.strlen

功能:计算字符串的长度

#include <stdio.h>
#include <string.h>
int main()
{
    int len;
    char name[]="sunwu kong";
    len = strlen(name);
    printf("%d",len);
    return 0;
}

结果:10,没有换行符,算空格;

2.strcpy

功能:拷贝一个字符串到另一个字符串

#include <stdio.h>
#include <string.h>
 
int main()
{
    char name[]="sunwukong";
    char ch[20];
    strcpy(ch,name);
    printf("%s",ch);
    return 0;
}

结果:sunwukong

3.strcat


把二个字符串拼起来

#include<stdio.h>
#include <string.h>
int main()
{
    char ai[20],ma[20];
    strcpy(ai,"The is one,");
    strcpy(ma,"the is two");
    strcat(ai,ma);
    printf("%s",ai);
    return 0;
}

结果:aima

4.strcmp

功能:比较两个字符串是否一样,比较的是ASCII 值

#include<stdio.h>
#include <string.h>
int main()
{
    char text[50],copy[50];
    strcpy(text,"The is one,");
    strcpy(copy,"the is two");
    if( strcmp(text,copy)> 0 )
    {
        printf("text 字符串ASCII 值大于 copy 字符串ASCII 值");
    }
    else if(strcmp(text,copy) < 0)
    {
        printf("text 字符串ASCII 值小于 copy 字符串ASCII 值");
    }
    else
    {
        printf("text 字符串ASCII 值等于 copy 字符串ASCII 值");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值