当前位置:网站首页>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
边栏推荐
猜你喜欢

R语言中实现作图对象排列的函数总结

Why disable foreign key constraints

WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令

What if the server is poisoned? How does the server prevent virus intrusion?

Cap theorem

Spark 算子之partitionBy

API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)

How can poor areas without networks have money to build networks?

Neodynamic Barcode Professional for WPF V11.0

New developments: new trends in cooperation between smartmesh and meshbox
随机推荐
Upgrade MySQL 5.1 to 5.67
网站压测工具Apache-ab,webbench,Apache-Jemeter
Spark 算子之partitionBy
Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK
编译,连接 -- 笔记
现在做自媒体能赚钱吗?看完这篇文章你就明白了
Cap theorem
Spark 算子之filter使用
Go语言条件,循环,函数
One brush 312 - simple repetition set - Sword finger offer 03 Duplicate number in array (E)
Upgrade MySQL 5.1 to 5.68
Fastjon2他来了,性能显著提升,还能再战十年
Advantages, disadvantages and selection of activation function
单体架构系统重新架构
Redis主从复制过程
携号转网最大赢家是中国电信,为何人们嫌弃中国移动和中国联通?
Redis master-slave replication process
Spark 算子之coalesce与repartition
【第5节 if和for】
计算某字符出现次数