#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void fun(char* a, int n)
{int i = 0, j = 0, m = 0,b=0,c=0;char* p;p = a;//第一步,判断字母前面有多少个*while (p[i++] == '*'){j++;}printf("字母前*的个数=%d\n",j);//求总的字符串长度while (a[m++] != '\0'){b++;}printf("字符串的总长度=%d\n", b);//是否满足n个//如果大于n,则删除多余的*//如果小于n,则不变if (n < j){//把多余的*删除//多余的*有j-n个for (i = j - n; i < b; i++){a[c++] = p[i];}}a[c] = '\0';
}void main()
{char s[81]; int n; printf("Enter a string:\n"); gets(s);printf("Enter n : "); scanf("%d", &n);fun(s, n);printf("The string after deleted:\n"); puts(s);
}
输出结果: