void fun(char *str) { int i, j, lead = 1; char new_str[1000]; for (i = 0, j = 0; str[i] != '\0'; i++) { if (lead && str[i] == '*') { continue; } else { lead = 0; } if (str[i] != '*') { new_str[j++] = str[i]; } } new_str[j] = '\0'; strcpy(str, new_str);}
时间: 2024-04-26 15:25:00 浏览: 22
这个函数的作用是将字符串中除了前导*号以外的所有*号删除,并将结果保存在原字符串中。
函数中的变量 i 和 j 分别用于遍历原字符串和新字符串。变量 lead 用于标记前导*号是否已经处理完毕,初始值为 1 表示还未处理前导*号。
在遍历原字符串时,如果 lead 等于 1 并且当前字符为*号,则说明当前字符是前导*号,直接跳过。否则将 lead 的值设置为 0 表示前导*号已经处理完毕。
如果当前字符不为*号,则将其复制到新字符串中。
最后,将新字符串的末尾加上一个空字符'\0',并使用 strcpy 函数将其拷贝回原字符串中。
相关问题
#include<stdio.h> int String(const char* str) { int len = 0; while ('\0' != str[len]) { len++; } return len; } void Fun(char* str) { int len = String(str); char cmp; for (int i = 0; i < len - 1; i++) { for (int j = 0; j < len - i - 1; j++) { if (str[j] > str[j + 1]) { cmp = str[j]; str[j] = str[j + 1]; str[j + 1] = cmp; } } } } int main() { char str[24]; printf("请输入一个字符串:"); scanf_s("%s", str, sizeof(str)); String(str); printf("排序后结果为:%s\n", str); }改错
改错如下:
```c
#include <stdio.h>
int String(const char* str)
{
int len = 0;
while ('\0' != str[len])
{
len++;
}
return len;
}
void Fun(char* str)
{
int len = String(str);
char cmp;
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (str[j] > str[j + 1])
{
cmp = str[j];
str[j] = str[j + 1];
str[j + 1] = cmp;
}
}
}
}
int main()
{
char str[24];
printf("请输入一个字符串:");
scanf("%s", str);
Fun(str);
printf("排序后结果为:%s\n", str);
return 0;
}
```
主要改动如下:
1. 添加了头文件 `stdio.h`。
2. 修改了 `scanf_s` 为 `scanf`,因为在没有指定缓冲区大小的情况下,两者功能是相同的。
3. 在 `main` 函数中调用了 `Fun` 函数对字符串进行排序。
4. 在 `main` 函数中添加了 `return 0` 语句,表示程序正常结束。
void sort(char **str_arr, int size)
该函数的作用是对字符串数组进行排序,可以采用快速排序或归并排序等算法实现。下面是一个使用快速排序的示例代码:
```
void swap(char **a, char **b) {
char *temp = *a;
*a = *b;
*b = temp;
}
int partition(char **arr, int low, int high) {
char *pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (strcmp(arr[j], pivot) < 0) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
void quickSort(char **arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void sort(char **str_arr, int size) {
quickSort(str_arr, 0, size - 1);
}
```
这里使用了一个 `swap` 函数来交换两个字符串指针,使用了 `strcmp` 函数比较两个字符串的大小,并且使用快速排序算法对字符串数组进行排序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)