c语言*****a*bc*def*g*******怎么去掉星号,请用C代码完成去掉字符串前后的星号?

这段代码展示了如何使用C语言编写一个函数`mytrim`,该函数去除字符串开头和结尾的空格及星号。当输入字符串全为空格或星号时,函数会特别提示。示例调用中包含了多种测试情况,包括全空格、全星号以及混合空格和星号的字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

满意答案

下面的代码对空格和星号都有效果,字符串中左边和右边所有的空格和星号都不会被打印:1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253#include <stdio.h>#include <string.h>  void mytrim(char* pStr){    char * pStart = pStr;    char * pEnd   = pStr;    int count = 0;    if(pStr==NULL || strlen(pStr) == 0)    {      printf("the string is NULL\n");      return ;    }          while (*pStart == ' ' || *pStart == '*')    {        pStart++;        count ++;    }    if(count == strlen(pStr))    {        printf("is all the * and space !\n");        return ;    }     while(*pEnd !='\0')        pEnd ++;    pEnd --;     while(*pEnd ==' ' || *pEnd =='*')        pEnd --;          while (pStart <= pEnd)    {       printf("%c",*pStart);       pStart++;    }    printf("\n"); }  int main(){    mytrim("            ");    mytrim("   ***  ****  ");    mytrim("**************");    mytrim("   ABC  AA * B  ");    mytrim("  *  * ABC**ABC AADD **  ****");    mytrim(" ********A*BC***DEF*G");    mytrim("********A*BC***DEF*G****");    return 0;}

输出结果:

is all the * and space !

is all the * and space !

is all the * and space !

ABC  AA * B

ABC**ABC AADD

A*BC***DEF*G

A*BC***DEF*G

00分享举报

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值