C语言基础之字符串处理函数

字符串处理函数

1、测量字符串长度strlen

	#include <string.h>
	size_t strlen(const char *s);

s指需要测量的字符串首元素地址

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

int main(int argc, char const *argv[])
{
    char str[128] = "hello world";
    printf("%d",strlen(str));
    return 0;
}

2、字符串拷贝函数strcpy、strncpy

(1)strcpy

    #include <string.h>
    char *strcpy(char *dest, const char *src);

​ dest:目的空间地址

​ src:原字符串首元素地址

​ 返回值:目的空间地址的首地址(新字符串地址)

​ 如果遇到’\0’,直接结束

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

int main(int argc, char const *argv[])
{
    char str[128] = "hello world";
    char str2[128] = "";
    strcpy(str2,str);
    printf("%s\n",str2);
    return 0;
}

(2)strncpy

#include <string.h>
char *strncpy(char *dest, const char *src, size_t n);

​ dest:目的空间地址

​ src:原字符串首元素地址

​ n :拷贝前n个,如果遇到’\0’,直接结束

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

int main(int argc, char const *argv[])
{
    char str[128] = "hello world";
    char str2[128] = "";
    strncpy(str2,str,6);
    printf("%s\n",str2);
    return 0;
}

3、字符串追加函数strcat、strncat

(1)strcat

#include <string.h>
char *strcat(char *dest, const char *src);

​ 将src指向的字符串追加到dest指向的字符串尾部

​ 如果遇到’\0’,直接结束

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

int main(int argc, char const *argv[])
{
    char dst[128] = "hello ";
    char str[128] = "world";
    strcat(dst,str);
    printf("%s\n",dst);
    return 0;
}

(2)strncat

#include <string.h>
char *strncat(char *dest, const char *src, size_t n);

​ 将src指向的前n个字符串追加到dest指向的字符串尾部

​ 如果遇到’\0’,直接结束

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

int main(int argc, char const *argv[])
{
    char dst[128] = "hello ";
    char str[128] = "world";
    strncat(dst,str,3);
    printf("%s\n",dst);
    return 0;
}

4、字符串比较函数strcmp、strncmp

(1)strcmp

#include <string.h>
int strcmp(const char *s1, const char *s2);

​ s1字符串 s2字符串

​ 返回值

​ >0 s1>s2

​ <0 s1<s2

​ =0 s1=s2

逐个字符比较,相同比较下一个,不同输出s1 - s2的ascll值

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

int main(int argc, char const *argv[])
{
    char dst[128] = "ab";
    char str[128] = "bb";
    printf("%d\n",strcmp(dst,str));
    return 0;
}

(2)strncmp

#include <string.h>
int strncmp(const char *s1, const char *s2, size_t n);

​ s1字符串 s2字符串

​ n:比较前n个

​ 返回值

​ >0 s1>s2

​ <0 s1<s2

​ == s1=s2

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

int main(int argc, char const *argv[])
{
    char dst[128] = "hello ";
    char str[128] = "world";
    printf("%d\n",strncmp(dst,str,3));
    return 0;
}

5、字符查找函数strchr、strrchr

(1)strchr

#include <string.h>
char *strchr(const char *s, int c);

​ 在字符串s 查找第一次出现的字符 c

​ 返回:

​ 成功返回地址

​ 失败返回NULL

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

int main(int argc, char const *argv[])
{
    char dst[] = "bvaa";
    char c = 'a';
    char *result = strchr(dst,c);
    printf("%d\n",result - dst);//打印字符所在位置
    return 0;
}

(2)strrchr

#include <string.h>
char *strrchr(const char *s, int c);

​ 在字符串s 查找最后一次出现的字符 c

​ 返回:

​ 成功返回地址

​ 失败返回NULL

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

int main(int argc, char const *argv[])
{
    char dst[] = "bvaaasd";
    char c = 'a';
    char *result = strrchr(dst,c);
    printf("%d\n",result - dst);//打印字符所在位置
    return 0;
}

6、字符串查找函数strstr

#include <string.h>
char *strstr(const char *haystack, const char *needle);

​ 返回值:

​ 找到返回找到的地址

​ 失败返回NULL

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

int main(int argc, char const *argv[])
{
    char str[] = "heloalol worlod";
    char *ret = strstr(str, "lol");
    if(ret != NULL)
    {
        printf("%s\n",ret);
        printf("%d\n",ret - str);//打印字符所在位置
    }
    return 0;
}

7、字符串转换

	#include <stdlib.h>

​ atoi将字符串 转成 int类型

​ atol将字符串 转成 long类型

​ atof将字符串 转成 float类型

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    printf("%d\n",atoi("11"));
    printf("%ld\n",atol("123"));
    printf("%f\n",atof("3.14f"));
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    char buf[] = "12345ass123gd";
    int sum = 0;
    int i = 0;
    while (buf[i]>='0' && buf[i]<='9')
    {
        sum = sum*10 + (buf[i] - '0');
        i++;
    }
    printf("%d\n",sum);
    return 0;
}

结果输出12345

8、字符串切割

(1)strtok

#include <string.h>
char *strtok(char *str, const char *delim);

​ 第一次切割:str必须指向 待切割的字符串的首元素地址

​ delim指向分割符”分割符”

​ 后续切割:str传空NULL

​ 返回值:

​ 成功返回子串的首元素地址

​ 失败返回NULL

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

int main(int argc, char const *argv[])
{
    char str[] = "hel,oalolw,orlo,dsdfs,df,xc,fdgd";
    char *buf[32] = {str};//存放子串首元素地址
    int i = 0;
    while (1)
    { 
        buf[i] = strtok(buf[i] , ",");
        if(buf[i] == NULL)
            break;
        i++;
    }
    i = 0;
    while (buf[i] != NULL)
    {
        printf("%s\n",buf[i++]);
    }
    return 0;
}

9、格式化字符串

组包:按照需要的格式 组成字符串

解包:解析特定格式的数据

(1)sprintf

将零散的数据 按照固定的格式 组成字符串

#include <stdio.h>
sprintf(char  *str, const char *format, ...);

sprintf函数会根据提供的格式字符串(format)将后面的参数(…)格式化为一个字符串,并将其存储在提供的目标字符串(str)中。

函数返回的是写入的字符数量,不包括字符串的结束符

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int year = 2023;
    int manth = 10;
    int day = 30;   
    char buf[128] = "";
    int len = sprintf(buf,"%d-%d-%d-",year,manth,day);
    printf("len = %d ,buf = %s\n",len,buf);
    return 0;
}

结果

len = 10 ,buf = 2023-10-30

#include <stdio.h>

int main(int argc, char const *argv[])
{
    char buf[128] = "";
    sprintf(buf,"%d",1234);
    printf("buf = %s\n",buf);
    return 0;
}

(2)sscanf

①sscanf 和%d 提取’0’~ ’9’数字
#include <stdio.h>
int main(int argc, char const *argv[])
{
    char buf[128] = "2023年7月26日";
    int year = 0;
    int manth = 0;
    int day = 0;    
    sscanf(buf , "%d年%d月%d日",&year,&manth,&day);
    printf("%d %d %d\n",year,manth,day);
    return 0;
}

结果

2023 7 26

②sscanf 和%s 遇到’\0’ (空格)结束
#include <stdio.h>
int main(int argc, char const *argv[])
{
  char buf[128] = "2023年 7月26日";
  char str[128] = ""; 
  sscanf(buf,"%s",str);
  printf("%s\n",str);  
  return 0;
}

结果

2023年

③sscanf高级用法

跳过数据 % * d ,% * s

#include <stdio.h>
int main(int argc, char const *argv[])
{
  char buf[128] = "";
  sscanf("123:e1343","%*d:%s",buf);
  printf("buf = %s\n",buf);
  return 0;
}

结果

buf = e1343

读取指定宽度的数据:%[width]s %[width]d

#include <stdio.h>
int main(int argc, char const *argv[])
{
    char buf[128] = "";
    sscanf("1231343","%3s",buf);//宽度限制
    printf("buf = %s\n",buf);
    return 0;
}

结果

buf = 123

%[a-z]表示匹配a到z的任意字符

遇到不是a-z之间的字符跳出(可加条件%[a-z,0-9,A-Z])

#include <stdio.h>
int main(int argc, char const *argv[])
{
  char buf[128] = "";
  sscanf("asd12ascHI313ASDxc43","%[a-z]",buf);
  printf("buf = %s\n",buf);
  return 0;
}

结果

buf = asd

贪婪性

%[asd]匹配a,s,d中任意一员,依次输出符合数据,遇到不符合的跳出

#include <stdio.h>
int main(int argc, char const *argv[])
{
    char buf[128] = "";
    sscanf("adsdadsddcHI313ASDxc43add","%[adds]",buf);
    printf("buf = %s\n",buf);
    return 0;
}

结果

buf = adsdadsdd

% [ ^ asd]匹配非a,s,d中任意一员,匹配到a,s,d中任意一员跳出

#include <stdio.h>
int main(int argc, char const *argv[])
{
  char buf[128] = "";
  sscanf("qwfbcHI313AaSDxc43","%[^add]",buf);
  printf("buf = %s\n",buf);
  return 0;
}

结果

buf = qwfbcHI313A

**案例:**输出指定位置字符

#include <stdio.h>
int main(int argc, char const *argv[])
{
    char buf[128] = "https://www.baidu.com";
    char name[128] = "";
    char log[128] = ""; 
    sscanf(buf,"%[^:]://www.%[^.]",name,log);
    printf("name= %s \n log= %s\n",name,log);
    return 0;
}

结果

name= https
log= baidu

(3)const

①修饰普通变量为只读变量
#include <stdio.h>
int main(int argc, char const *argv[])
{
    const int num = 10;
    num = 100;//不允许修改只允许初始化
    printf("%d\n",num);
    return 0;
}
②修饰*

( * p只读 不能通过* p修改所指向的空间内容,p仍可读可写)

#include <stdio.h>
int main(int argc, char const *argv[])
{
    int num = 10;
    int data = 20;
    const *p =&num;
    num =100;
    //*p =12;//不能修改
    p = &data;//可读可写
    printf("%d\n",num);
    return 0;
}
③修饰 p

p不可修改 *p可修改

#include <stdio.h>
int main(int argc, char const *argv[])
{
    int num = 10;

    int *const p =&num;
    *p =100;
    printf("%d\n",num);
    return 0;
}

10、如有错误欢迎指正

相关推荐

  1. C语言基础字符串处理函数

    2024-03-15 09:12:02       21 阅读
  2. C++语言学习(四)—— 字符串处理函数

    2024-03-15 09:12:02       14 阅读
  3. C语言 字符串处理相关函数大汇总(16~20)

    2024-03-15 09:12:02       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-15 09:12:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-15 09:12:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-15 09:12:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-15 09:12:02       20 阅读

热门阅读

  1. 新概念英语第二册(83)

    2024-03-15 09:12:02       20 阅读
  2. vue 修改props

    2024-03-15 09:12:02       22 阅读
  3. react hook: useReducer

    2024-03-15 09:12:02       21 阅读
  4. 最短路 算法解析+例题

    2024-03-15 09:12:02       20 阅读
  5. html--bug

    html--bug

    2024-03-15 09:12:02      21 阅读
  6. c++字符串刷题:整数反转

    2024-03-15 09:12:02       19 阅读