满意答案
下面的代码对空格和星号都有效果,字符串中左边和右边所有的空格和星号都不会被打印: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分享举报