当前位置:网站首页>[C语言]压缩字符串并添加标记字符
[C语言]压缩字符串并添加标记字符
2022-06-11 17:59:00 【跳舞的皮埃尔】
1、题目
小科最近在研究字符串压缩标记问题,他想在给定的字符串的指定字符前面插入标记字符,若指定字符连续出现则需要把连续的字符压缩为1个字符加出现的次数,指定字符和标记字符均从键盘输入。请你帮他解决这个需求吧。
【输入形式】输入主串 s(长度小于100),输入指定字符 t,输入标记字符 c。
【输出形式】输出插入后字符串
样例1:
输入:
abbcabcde
b
*
输出:
a* b2ca* bcde
样例2:
输入:
abbcbbbe
b
c
输出:
acb2ccb3e
2、完整代码
#define _CRT_SECURE_NO_WARNINGS // Visual Studio版本太新需加上
#include <stdio.h>
#define N 100
char res[N];
// 压缩字符串
int Compress(char* arr)
{
int i = 0;
int j = 0;
int cout = 1;
while (arr[i] != '\0')
{
if (arr[i] == arr[i + 1])
{
cout++;
}
else
{
if (cout == 1) {
arr[j++] = arr[i];
}
else if (cout != 1) {
arr[j++] = arr[i];
arr[j++] = cout + '0';
}
cout = 1;
}
i++;
}
arr[j] = '\0';
}
//添加标记字符
char* InsertStr(char* s, char t, char c)
{
int length = strlen(s);
int i = 0;
int count = 0;
while (i < length)
{
if (s[i] == t)
{
res[count++] = c;
}
res[count++] = s[i];
i++;
}
if (s[length - 1] == t) {
res[count++] = c;
}
res[count] = '\0';
return res;
}
int main()
{
char a[100], b, c;
gets(a);
Compress(a);
b = getchar();
scanf("\n");
c = getchar();
char* p = InsertStr(a, b, c);
puts(p);
return 0;
}
3、截图

边栏推荐
- Ffmpeg hardware codec NVIDIA GPU
- SQL语句当查询条件为空时默认查询全部数据,不为空是则按照条件进行查询
- 7-2 h0107. Pig-Latin
- Spring 2021 daily question [week6 not finished]
- mariadb spider分片引擎初体验
- General terms in security field
- ISCSI详解(四)——ISCSI服务端配置实战
- mariadb spider分片引擎初體驗
- [collect first and use it sooner or later] 49 Flink high-frequency interview questions series (II)
- 6-5 count the number of words (file) (*)
猜你喜欢

SISO Decoder for a General (n, N - 1) SPC Code (Supplementary section 3)

10 ways to reset any user password

密码学概述
![Acwing game 40 [End]](/img/6b/a9ea5e1f9578d21219c69571e8b0f8.jpg)
Acwing game 40 [End]

mysql8安装,navicat安装,sqli-labs搭建

New work of "the father of LSTM": a new method towards self correcting neural network

Say no to credit card fraud! 100 lines of code to realize simplified real-time fraud detection

安装mariadb 10.5.7(tar包安装)

Retrofit source code analysis

Mysql8 installation, Navicat installation, sqli labs setup
随机推荐
vulhub
Comparison of mongoose in express, KOA and egg
6-3 batch sum (*)
Hello go (XIV). Go language common standard library IV
zabbix怎样自定义mysql监控项并触发告警
Global and Chinese market of high frequency bipolar junction transistors 2022-2028: Research Report on technology, participants, trends, market size and share
安全领域常规术语
PIL-Pillow图像处理【1】-安装与新建
The tle6389 step-down DC-DC switch controller has high efficiency in the whole load range of 1mA to 2.5A - keshijin mall
Ffmpeg hard codec inter QSV
The top ten trends of 2022 industrial Internet security was officially released
如何学习和自学
Global and Chinese markets for ultra high speed printers 2022-2028: Research Report on technology, participants, trends, market size and share
Upload labs failed to pass the customs halfway and the middle road collapsed
TestPattern error
【C】 ATOI function implementation +offsetof implementation + exchange binary odd and even digits
Secret comment-----
About element location and size
[document operation] of ACM
【先收藏,早晚用得到】100个Flink高频面试题系列(二)