当前位置:网站首页>[c language] compress strings and add markup characters
[c language] compress strings and add markup characters
2022-06-11 18:17:00 【Dancing Pierre】
[C Language ] Compress the string and add the tag character
1、 subject
Xiaoke recently studied the string compression tag problem , He wants to insert a marker character before a specified character in a given string , If the specified characters appear continuously, you need to compress the continuous characters into 1 Characters plus the number of occurrences , Both the specified character and the marking character are entered from the keyboard . Please help him solve this need .
【 Input form 】 Enter the main string s( The length is less than 100), Enter the specified character t, Enter marker character c.
【 Output form 】 Output the inserted string
Examples 1:
Input :
abbcabcde
b
*
Output :
a* b2ca* bcde
Examples 2:
Input :
abbcbbbe
b
c
Output :
acb2ccb3e
2、 Complete code
#define _CRT_SECURE_NO_WARNINGS // Visual Studio The version is too new and needs to be added
#include <stdio.h>
#define N 100
char res[N];
// Compress string
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';
}
// Add marker character
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、 Screenshot

边栏推荐
- Codeworks round 479 (Div. 3) [done]
- 社会工程学实战入门
- Seeing the sudden death of a 28 year old employee, I was silent
- Can 400 fans earn 20W a month? How did you do it?
- MMA-Self-defining function
- Feign 共享登录信息进行请求
- Upload labs failed to pass the customs halfway and the middle road collapsed
- SISO Decoder for a General (n, N - 1) SPC Code (Supplementary section 3)
- “LSTM之父”新作:一种新方法,迈向自我修正的神经网络
- 如何学习和自学
猜你喜欢

DC-DC自举电容(BOOT)几个问题

Cryptology Summary

LDPC 7 - 解码简单例子
![Acwing game 40 [End]](/img/6b/a9ea5e1f9578d21219c69571e8b0f8.jpg)
Acwing game 40 [End]
![[C语言]用结构体把最高分的学生输出,可有多个最高分](/img/4e/836a8f717a2d9bf5f999a934ff4c91.png)
[C语言]用结构体把最高分的学生输出,可有多个最高分
![[piecemeal knowledge] [network composition] the mobile phone can be connected to the campus network, but the computer can't](/img/a1/7858a0651ddca0dfd187dc128b2036.jpg)
[piecemeal knowledge] [network composition] the mobile phone can be connected to the campus network, but the computer can't

网络和并发编程常见面试题

RadioGroup动态添加RadioButton

【C】 ATOI function implementation +offsetof implementation + exchange binary odd and even digits

網絡安全威脅情報體系
随机推荐
SQL error injection 1
[document operation] of ACM
LeetCode_ Prefix tree_ Medium_ 208. implement trie (prefix tree)
There are three standards and three methods, i.e. fast growth and quick realization. How to choose the direction for making short videos of we media?
SQL报错注入1
[C语言]限制查找次数,输出次数内查找到的最大值
[piecemeal knowledge] [network composition] the mobile phone can be connected to the campus network, but the computer can't
密评-----
Common shortcut keys for Hello go (x) and GoLand
System learning typescript (V) - joint type
软件需求工程复习
Global and Chinese markets for ultra high speed printers 2022-2028: Research Report on technology, participants, trends, market size and share
在同花顺上面开股票账户好不好,安不安全?
神经网络与深度学习-2- 机器学习简单示例-PyTorch
排序的循环链表
Mysql8 installation, Navicat installation, sqli labs setup
EditText amount limit
H.264概念
Global and Chinese market of web content management software 2022-2028: Research Report on technology, participants, trends, market size and share
EditText 金额限制