#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);
}
輸出結果: