假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:只删除字符串前导和尾部的*号,串中字母之间的*号都不删除。 形参n 给出了字符串的长度,形参h给出了字符串中前导*号的个数, 形参e给出了字符串中最后的*个数。在编写时不得使用C语言给提供得字符串函数。

/假定输入的字符串中只包含字母和号。请编写函数fun,它的功能是:只删除字符串前导和尾部的号,串中字母之间的号都不删除。
形参n 给出了字符串的长度,形参h给出了字符串中前导号的个数,
形参e给出了字符串中最后的
个数。在编写时不得使用C语言给提供得字符串函数。 */

#include <stdio.h>

void fun(char str[], int n, int h, int e)
{
    int start = h;
    int end = n - 1 - e;
    while (str[start] == '*')
    {
        start++;
    }
    while (str[end] == '*')
    {
        end--;
    }
    int i, j;
    for (i = start, j = h; i <= end; i++, j++)
    {
        str[j] = str[i];
    }
    for (int k = h + end - start + 1; k < n; k++)
    {
        str[k] = '*';
    }
    str[h + end - start + 1] = '\0';
}

int main()
{
    int n = 16;
    int h = 3;
    int e = 4;
    char str[] = "*****hello**world***";
    printf("原始字符串: %s\n", str);
    fun(str, n, h, e);
    printf("处理后的字符串: %s\n", str);
    return 0;
}

posted on 2024-06-26 23:25  wessf  阅读(68)  评论(0)    收藏  举报