C语言字符串操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

void *memchr(const void *_s, int _c, size_t _n);

int memcmp(const void *_s1, const void *_s2, size_t _n);
//
void *memcpy(void *_s1, const void *_s2, size_t _n);

void *memmove(void *_s1, const void *_s2, size_t _n);

void *memset(void *_s, int _c, size_t _n);
//字符串拼接
char *strcat(char *_s1, const char *_s2);
//一个字符在一个字符串中首次出现的位置
char *strchr(const char *_s, int _c);
//一个字符在一个串中最后一次出现的位置
char *strrchr(const char *_s, int _c);
//字符串比较
int strcmp(const char *_s1, const char *_s2);
//字符串比较,只比较n位
int strncmp(const char *_s1, const char *_s2, size_t _n);

int strcoll(const char *_s1, const char *_s2);

char *strcpy(char *_s1, const char *_s2);

size_t strcspn(const char *_s1, const char *_s2);

char *strerror(int _errnum);
//字符串长度
size_t strlen(const char *_s);

char *strncat(char *_s1, const char *_s2, size_t _n);

char *strncpy(char *_s1, const char *_s2, size_t _n);

char *strpbrk(const char *_s1, const char *_s2);


size_t strspn(const char *_s1, const char *_s2);
//字符串查找
char *strstr(const char *_s1, const char *_s2);

char *strtok(char *_s1, const char *_s2);

size_t strxfrm(char *_s1, const char *_s2, size_t _n);

strlen(字符串长度)

strcmp(比较字符串大小)

strcpy(字符串拷贝)

strcat(追加字符串)

strstr(字符查找)

1
2
char *strstr(const char *str1, const char *str2);
功能:返回str2第一次在str1中的位置,如果没有找到,返回NULL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<string.h>
#include<stdio.h>
typedef long long ll ;
int main(){
char cs[10000] ="hellojava hellocpp";
char finds[]="";
char *c = strstr(cs,"ll");
//原字符串
printf("%s\n",cs);
printf("\n");
//出现ll的位置
printf("%d\n",(int)(c-cs));
//剩余的字符串
printf("%s\n",c) ;
return 0 ;
}

sscanf正则

1
2
3
4
5
% 代表选择,后跟相应条件
%* 代表过滤(即连续匹配满足条件的字符,并丢弃)
%及%* 后面紧跟的数字代表匹配的字符个数
%5[^)]代表匹配5个非)的字符,[]内是筛选的条件,^表示否定,如%[a-z]表示接收小写字母,%[A-Z0-9] 表示接收大写字母及数字
要匹配中间的字符时,应先将前面的字符用%*过滤掉