当前位置:网站首页>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
边栏推荐
- Mobile finance (for personal use)
- 导入地址表分析(根据库文件名求出:导入函数数量、函数序号、函数名称)
- MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
- Spark 算子之partitionBy
- Go语言切片,范围,集合
- cadence SPB17. 4 - Active Class and Subclass
- JVM-第2章-类加载子系统(Class Loader Subsystem)
- 多级缓存使用
- Partitionby of spark operator
- Temporal model: long-term and short-term memory network (LSTM)
猜你喜欢
Spark 算子之distinct使用
Codejock Suite Pro v20.3.0
The principle and common methods of multithreading and the difference between thread and runnable
WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile
多级缓存使用
[open source tool sharing] MCU debugging assistant (oscillograph / modification / log) - linkscope
Basic concepts of website construction and management
Mobile finance (for personal use)
Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
C language --- string + memory function
随机推荐
【第5节 if和for】
Deeply learn the skills of parameter adjustment
R语言中绘制ROC曲线方法二:pROC包
Multitimer V2 reconstruction version | an infinitely scalable software timer
Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
[split of recursive number] n points K, split of limited range
Go并发和通道
Independent operation smart farm Innovation Forum
fatal error: torch/extension. h: No such file or directory
Mumu, go all the way
Why disable foreign key constraints
Merging of Shanzhai version [i]
Do we media make money now? After reading this article, you will understand
s16.基于镜像仓库一键安装containerd脚本
Go language, condition, loop, function
幂等性的处理
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
Basic greedy summary
Timing model: gated cyclic unit network (Gru)
多生成树MSTP的配置