C语言题:删除字符串中尾部的字符

来源:百度知道 编辑:UC知道 时间:2024/06/08 10:10:42
假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:使字符串中尾部的*号不得多于n个;若多于n个,则删除多于的*号;若少于或等于n个,则什么也不做,字符串中间和前面的*号不删除

#include<stdio.h>

#define N 5 // 定义最多 * 的个数
int fun(char *s,int n)
{ char *max;
int con_num=0; //连续出现 * 的次数
while(*s != '\0')
{ if(*s=='*')
{ con_num++;
if(con_num == n) max = s; //连续出现 n 次,记录该位置
}
else con_num=0; // 若不是 * 则重新计数
s++;
}
if(con_num>=n)
{ *(max+1) = '\0'; //删除多余的 * 字符
return 1;
}
else return 0;
}
void main()
{
char str[100];
scanf("%s",str);
if(fun(str,N)) printf("\n已删除!处理后的字符串:%s\n",str);
else printf("\n未删除:%s\n",str);
}

代码如下:
#include <stdio.h>
#include <string.h>
int main()
{
char buff[50];
char buff1[50];
scanf("%s",&buff);
for(int i=0;i<strlen(buff)-1;i++)
{
buff1[i]=buff[i];
}
for(int i=