压缩字符串,用户输入一个字符串,将挨着的并且相同的字符压缩 原字符串: abbcccdfttttty 压缩后: a2b3cdf5ty
时间: 2024-02-22 22:00:53 浏览: 22
好的,这是一个字符串压缩的问题。我可以通过遍历字符串,记录当前字符和它出现的次数来实现压缩。
以下是一个Python代码示例:
```python
def compress_string(s):
if not s:
return ""
result = ""
current_char = s[0]
count = 1
for i in range(1, len(s)):
if s[i] == current_char:
count += 1
else:
result += current_char + str(count)
current_char = s[i]
count = 1
result += current_char + str(count)
return result
```
这个函数会遍历输入的字符串,记录当前字符和它出现的次数。当遇到一个新字符时,就将之前的字符和出现次数添加到结果中,并更新当前字符和计数器。最后,需要将最后一个字符和出现次数添加到结果中。
例如,使用该函数对字符串"abbcccdfttttty"进行压缩,会得到"a2b2c3d1f1t4y1"。
相关问题
使用java帮我编写一段代码,压缩字符串,用户输入一个字符串,将挨着的并且相同的字符压缩 原字符串: abbcccdfttttty 压缩后: a2b3cdf5ty
好的,以下是使用 Java 编写的压缩字符串的代码:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个字符串:");
String str = scanner.nextLine();
StringBuilder result = new StringBuilder();
char prev = str.charAt(0);
int count = 1;
for (int i = 1; i < str.length(); i++) {
char curr = str.charAt(i);
if (curr == prev) {
count++;
} else {
result.append(prev);
if (count > 1) {
result.append(count);
}
prev = curr;
count = 1;
}
}
result.append(prev);
if (count > 1) {
result.append(count);
}
System.out.println("压缩后的字符串为:" + result.toString());
}
}
```
运行结果如下:
```
请输入一个字符串:abbcccdfttttty
压缩后的字符串为:a2b3cdf5ty
```
代码中使用 `StringBuilder` 类来拼接压缩后的字符串,使用一个计数器 `count` 来统计连续相同字符的个数。遍历字符串时,如果当前字符与前一个字符相同,则将计数器加 1;否则将前一个字符及其连续出现的次数(如果大于 1)添加到结果字符串中,并将计数器重置为 1。最后需要将最后一个字符及其连续出现的次数(如果大于 1)添加到结果字符串中。
压缩字符串,用户输入一个字符串,将挨着的并且相同的字符压缩 原字符串: abbcccdfttttty 压缩后: a2b3cdf5ty利用c语言写出一个代码给我
好的,以下是一个使用 C 语言实现字符串压缩的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* compress_string(char* s) {
if (!s || !strlen(s)) {
return "";
}
char* result = (char*) malloc(sizeof(char) * (strlen(s) * 2 + 1));
char current_char = s[0];
int count = 1;
int index = 0;
for (int i = 1; i < strlen(s); i++) {
if (s[i] == current_char) {
count++;
} else {
index += sprintf(&result[index], "%c%d", current_char, count);
current_char = s[i];
count = 1;
}
}
index += sprintf(&result[index], "%c%d", current_char, count);
result[index] = '\0';
return result;
}
int main() {
char s[] = "abbcccdfttttty";
char* result = compress_string(s);
printf("Original string: %s\n", s);
printf("Compressed string: %s\n", result);
free(result);
return 0;
}
```
该代码中,我们首先判断输入字符串是否为空,然后利用 `malloc()` 分配一个足够大的字符数组来存储压缩后的字符串。接着,我们遍历输入字符串,记录当前字符和它出现的次数。当遇到一个新字符时,就将之前的字符和出现次数添加到结果中,并更新当前字符和计数器。最后,需要将最后一个字符和出现次数添加到结果中,并将结果字符串的最后一个字符设为 NULL,以表示字符串的结束。
例如,使用该函数对字符串"abbcccdfttttty"进行压缩,会得到"a2b3c3d1f1t4y1"。
相关推荐
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)