当前位置:网站首页>[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、截图

边栏推荐
- ISCSI详解(四)——ISCSI服务端配置实战
- New work of "the father of LSTM": a new method towards self correcting neural network
- Radiogroup dynamically add RadioButton
- 6-5 count the number of words (file) (*)
- 剑指 Offer(第 2 版)
- 夜神安装apk,以及bp代理
- [FAQs for novices on the road] about project management
- Understanding of distributed transactions
- Global and Chinese markets for ultra high speed printers 2022-2028: Research Report on technology, participants, trends, market size and share
- Spring 2021 daily question [week5 not finished]
猜你喜欢

Ffmpeg parity field frame interlace progressive command and code processing

【先收藏,早晚用得到】100个Flink高频面试题系列(三)

Sqli labs customs clearance hey hey~

ACL 2022: is it no longer difficult to evaluate word polysemy? A new benchmark "dibimt"
![Winter vacation daily question (improvement group) [end of week 4]](/img/67/89b5164712d8c4eb319b9266dd4b91.jpg)
Winter vacation daily question (improvement group) [end of week 4]

upload-labs通关未半而中道崩殂

How to learn and self-study

async导致函数结果出乎意料,改变原来代码的意图;await is only valid in async functions and the top level bodies of modules
![[collect first and use it sooner or later] 49 Flink high-frequency interview questions series (II)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[collect first and use it sooner or later] 49 Flink high-frequency interview questions series (II)

“LSTM之父”新作:一种新方法,迈向自我修正的神经网络
随机推荐
async导致函数结果出乎意料,改变原来代码的意图;await is only valid in async functions and the top level bodies of modules
SISO decoder for a general (n,n-1) SPC code(补充章节3)
Hello go (XI). Go language common standard library I
Simple understanding of events
Is it good or not to open a stock account on the flush? Is it safe?
【C】 Compilation preprocessing and environment
vulhub
How ZABBIX can customize MySQL monitoring items and trigger alarms
6-1 how many words are needed to form a sentence?
安全领域常规术语
Install MariaDB 10.5.7 (tar package installation)
Initial egg framework
Acwing game 40 [End]
【先收藏,早晚用得到】100个Flink高频面试题系列(二)
mariadb spider分片引擎初體驗
[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (III)
The top ten trends of 2022 industrial Internet security was officially released
Global and Chinese market of web content management software 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of high frequency bipolar junction transistors 2022-2028: Research Report on technology, participants, trends, market size and share
General terms in security field