当前位置:网站首页>C语言自编字符串处理函数——字符串分割、字符串填充等
C语言自编字符串处理函数——字符串分割、字符串填充等
2022-04-23 15:48:00 【MallocLu】
字符串填充使用示例
#include <stdio.h>
#include <stdlib.h>
#include "CStringTools.h"
int main()
{
char cmd[1000];
char cmdList[50][20];
while (1)
{
scanf("%s", cmd);
if (strcmp(cmd, "exit") == 0)
break;
fillWithKeys(cmd, "*#", ' ');
printf("%s\n\n", cmd);
}
return 0;
}
字符串分割使用示例
#include <stdio.h>
#include <stdlib.h>
#include "CStringTools.h"
int main()
{
char cmd[1000];
char cmdList[50][20];
while (1)
{
scanf("%s", cmd);
if (strcmp(cmd, "exit") == 0)
break;
int cnt = split(cmdList, cmd, "*#");
for (int i = 0; i < cnt; ++i)
printf("%d\t\t%s\n", i+1, cmdList[i]);
printf("\n");
}
return 0;
}
工具函数代码
有.h和.c两个文件,将其加入到你的项目中,并且#include “CStringTools.h”,既可以使用其中的功能。
// CStringTools.h文件
#ifndef CSTRINGTOOLS_H_INCLUDED
#define CSTRINGTOOLS_H_INCLUDED
#include <string.h>
#include <malloc.h>
//在str指定字符keys前后填充一个content
void fillWithKeys(char *str, char* keys, char content);
//将字符串str按照分隔符keys分割成strList(上限50个字符串 长度20)
//返回实际分成的字符串的数量
int split(char strList[][20], char *str, char *keys);
#endif // CSTRINGTOOLS_H_INCLUDED
// CStringTools.c文件
#include "CStringTools.h"
void fillWithKeys(char *str, char* keys, char content)
{
char *tmp = (char *)malloc(sizeof(char) * strlen(str));
strcpy(tmp, str);
int index = 0;
for (int i = 0; i < strlen(tmp); ++i)
{
int flag = 0;
for (int j = 0; j < strlen(keys); ++j)
{
if (keys[j] == tmp[i])
{
flag = 1;
break;
}
}
if (flag)
str[index++] = content;
str[index++] = tmp[i];
if (flag)
str[index++] = content;
}
str[index] = '\0';
free(tmp);
}
int split(char strList[][20], char *str, char *keys)
{
int cnt = 0;
str[strlen(str)] = keys[0];
str[strlen(str)] = '\0';
int start = -1;
for (int i = 0; i < strlen(str); ++i)
{
int flag = 0;
for (int j = 0; j < strlen(keys); ++j)
{
if (keys[j] == str[i])
{
flag = 1;
break;
}
}
if(flag)
{
if (i - start > 1)
{
//切割[start+1,i-1]
for (int j = start + 1; j <= i - 1; ++j)
{
strList[cnt][j-start-1] = str[j];
}
strList[cnt][i - start - 1] = '\0';
++cnt;
}
start = i;
}
}
return cnt;
}
版权声明
本文为[MallocLu]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42283621/article/details/124364562
边栏推荐
- String sorting
- For examination
- Spark 算子之coalesce与repartition
- VIM specifies the line comment and reconciliation comment
- 【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷
- Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK
- s16. One click installation of containerd script based on image warehouse
- Go并发和通道
- Upgrade MySQL 5.1 to 5.66
- How can poor areas without networks have money to build networks?
猜你喜欢
mysql乐观锁解决并发冲突
Basic concepts of website construction and management
携号转网最大赢家是中国电信,为何人们嫌弃中国移动和中国联通?
CVPR 2022 优质论文分享
Mobile finance (for personal use)
Best practices of Apache APIs IX high availability configuration center based on tidb
大型互联网为什么禁止ip直连
What if the server is poisoned? How does the server prevent virus intrusion?
Spark 算子之filter使用
JVM - Chapter 2 - class loader subsystem
随机推荐
Spark 算子之groupBy使用
【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷
【递归之数的拆分】n分k,限定范围的拆分
VIM specifies the line comment and reconciliation comment
Control structure (I)
Accumulation of applet knowledge points
pywintypes.com_error: (-2147221020, ‘无效的语法‘, None, None)
Neodynamic Barcode Professional for WPF V11.0
vim指定行注释和解注释
IronPDF for . NET 2022.4.5455
Go并发和通道
山寨版归并【上】
Go language, condition, loop, function
c语言---字符串+内存函数
mysql乐观锁解决并发冲突
现在做自媒体能赚钱吗?看完这篇文章你就明白了
The length of the last word of the string
Merging of Shanzhai version [i]
R语言中绘制ROC曲线方法二:pROC包
Neodynamic Barcode Professional for WPF V11. 0