C 字符串处理

比较常用的几个字符串处理方法:

1.字符串拼接  strcat

2.判断字符串是否相等 strcmp/ strcasecmp

3.字符串复制strcpy 

4.获取字符串长度 strnlen

------------------------------------------------------------------------------

#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;

int main() {
    char a[] = "我要";
    char b[] = "KK";

    char  *site;
    //1.字符串拼接
    site = strcat(a,b);
    cout<<"输出拼接的字符串:"<<site << endl;

    //2.判断字符串是否相等
    //2.1 strcmp 判断字符串是否相等
    char *str1;
    if(strcmp(a,b)==0){
        cout<<"strcmp  a=b : ture"<< endl;
    }else{
        cout<<"strcmp  a!=b : false"<< endl;
    }
    //2.2 strcasecmp 判断字符串是否相等
    char *c = "you";
    char *d = "you";
    if(strcasecmp(c,d)==0){
        cout<<"strcasecmp  c=d : ture"<< endl;
    }else{
        cout<<"strcasecmp  c!=d: false"<< endl;
    }

    //strcpy()函数:是将一个字符串复制到另一块空间地址中 的函数,‘\0’是停止拷贝的终止条件,同时也会将 '\0' 也复制到目标空间。下面是库中的strcpy()函数声明:
    //char* strcpy(char* destination,const char* source);
    //1. 函数的参数:
    //char* destination---------目标字符串的首地址
    //const char* source------源地址:被复制的字符串的首地址,用const修饰,避免修改掉被拷贝的字符串

    //3.将一个字符串复制到另一块空间地址中(会覆盖目标地址原有的值)
    char e[] = "c123a";
    char *f= "d456b";
    strcpy(e,f);
    cout<< e << " --- "<<f<<endl;

    //4.strlen 获取字符串长度
    char *h = "abcdefghigkl";
    int n = strlen(h);
    cout<<"长度=" << n <<endl;

    //5.strnlen 获取字符串长度
    //获取字符串s中实际字符个数,不包括结尾的'\0';如果实际个数 <= maxlen,则返回n,否则返回第二个参数
    cout<<strnlen(h, 10)<<endl;


    char arr1[10] = "abasfsdfs";
    char arr2[10] = "abcd";
    printf("%s\n", strcpy(arr1, arr2));

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LanComer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值