当前位置:网站首页>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
边栏推荐
- Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
- Leetcode-396 rotation function
- Accumulation of applet knowledge points
- String sorting
- 基础贪心总结
- One brush 313 sword finger offer 06 Print linked list from end to end (E)
- Treatment of idempotency
- Large factory technology implementation | industry solution series tutorials
- Sortby use of spark operator
- R语言中实现作图对象排列的函数总结
猜你喜欢

Spark 算子之groupBy使用

Temporal model: long-term and short-term memory network (LSTM)

Load Balancer

单体架构系统重新架构

多级缓存使用

实现缺省页面

The principle and common methods of multithreading and the difference between thread and runnable

【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope

一文掌握vscode远程gdb调试

Tencent offer has been taken. Don't miss the 99 algorithm high-frequency interview questions. 80% of them are lost in the algorithm
随机推荐
Go language slice, range, set
怎么看基金是不是reits,通过银行购买基金安全吗
Leetcode-396 rotation function
String sorting
Application case of GPS Beidou high precision satellite time synchronization system
Treatment of idempotency
【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
Go concurrency and channel
导入地址表分析(根据库文件名求出:导入函数数量、函数序号、函数名称)
R语言中绘制ROC曲线方法二:pROC包
JVM-第2章-类加载子系统(Class Loader Subsystem)
Leetcode-374 guess the size of the number
Filter usage of spark operator
APISIX jwt-auth 插件存在错误响应中泄露信息的风险公告(CVE-2022-29266)
北京某信护网蓝队面试题目
Load Balancer
Unity Shader学习
Configuration of multi spanning tree MSTP
Best practices of Apache APIs IX high availability configuration center based on tidb
多线程原理和常用方法以及Thread和Runnable的区别