当前位置:网站首页>C language self compiled string processing function - string segmentation, string filling, etc
C language self compiled string processing function - string segmentation, string filling, etc
2022-04-23 15:51:00 【MallocLu】
List of articles
String padding usage example
#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;
}
Example of string segmentation
#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;
}
Tool function code
Yes .h and .c Two documents , Add it to your project , also #include “CStringTools.h”, You can use the functions .
// CStringTools.h file
#ifndef CSTRINGTOOLS_H_INCLUDED
#define CSTRINGTOOLS_H_INCLUDED
#include <string.h>
#include <malloc.h>
// stay str Specify the characters keys Fill one before and after content
void fillWithKeys(char *str, char* keys, char content);
// The string str According to the separator keys Divided into strList( ceiling 50 A string length 20)
// Returns the number of strings actually divided into
int split(char strList[][20], char *str, char *keys);
#endif // CSTRINGTOOLS_H_INCLUDED
// CStringTools.c file
#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)
{
// cutting [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://yzsam.com/2022/04/202204231548205933.html
边栏推荐
- dlopen/dlsym/dlclose的简单用法
- Calculate the number of occurrences of a character
- 山寨版归并【上】
- Tencent offer has been taken. Don't miss the 99 algorithm high-frequency interview questions. 80% of them are lost in the algorithm
- Codejock Suite Pro v20.3.0
- Use bitnami PostgreSQL docker image to quickly set up stream replication clusters
- Why is IP direct connection prohibited in large-scale Internet
- c语言---指针进阶
- Spark 算子之coalesce与repartition
- shell_2
猜你喜欢
R语言中绘制ROC曲线方法二:pROC包
CVPR 2022 quality paper sharing
New developments: new trends in cooperation between smartmesh and meshbox
C, calculation method and source program of bell number
Treatment of idempotency
一刷314-剑指 Offer 09. 用两个栈实现队列(e)
Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
R语言中实现作图对象排列的函数总结
For examination
MySQL集群模式与应用场景
随机推荐
Go语言条件,循环,函数
使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群
【递归之数的拆分】n分k,限定范围的拆分
Fastjon2他来了,性能显著提升,还能再战十年
Interview questions of a blue team of Beijing Information Protection Network
utils. Deprecated in35 may be cancelled due to upgrade. What should I do
Extract non duplicate integers
pgpool-II 4.3 中文手册 - 入门教程
C, calculation method and source program of bell number
Partitionby of spark operator
The length of the last word of the string
MySQL集群模式与应用场景
MySQL Cluster Mode and application scenario
为啥禁用外键约束
Large factory technology implementation | industry solution series tutorials
pywintypes.com_error: (-2147221020, ‘无效的语法‘, None, None)
CVPR 2022 quality paper sharing
Cookie&Session
Config learning notes component
[open source tool sharing] MCU debugging assistant (oscillograph / modification / log) - linkscope