1. 程式人生 > >字符串處理:去除首部空格,去除中間多余空格,在數字和字母中間插入指定字符串

字符串處理:去除首部空格,去除中間多余空格,在數字和字母中間插入指定字符串

bool har bsp while 打印 col spa nbsp pri

 1 #include<cstdio>
 2 #include<cstring>
 3 bool isN(char c){//判斷該字符是否是數字 
 4     if(c>=0&&c<=9) return true;
 5     else return false;
 6 }
 7 bool isW(char c){//判斷該字符是否是字母 
 8     if((c>=a&&c<=z)||(c>=A&&c<=Z))
 9     return true;
10     else
return false; 11 } 12 int main(){ 13 char c[110]; 14 while(gets(c)){ 15 int len=strlen(c); 16 char *p=c; 17 while(*p== ){//去除字符串前面的所有空格 18 p++; 19 } 20 int k=0; 21 while(*p!=\0){//將字符串中的多余空格去除,保留一個空格 22 c[k++]=*p++; 23 while
(c[k-1]== && *p== ){ 24 p++; 25 } 26 } 27 char t,d; 28 for(int i=0;i<k;i++){ 29 t=c[i];d=c[i+1]; 30 if((isN(t)==true&&isW(d)==true)||(isN(d)==true&&isW(t)==true)){ 31 printf("
%c",t);//在字符和數字中間插入相應的字符 32 if(i!=k-1){//如果打印的不是最後一個字符,那麽久打印指定字符 33 printf("_"); 34 } 35 }else{ 36 printf("%c",t); 37 } 38 } 39 printf("\n"); 40 } 41 return 0; 42 }

字符串處理:去除首部空格,去除中間多余空格,在數字和字母中間插入指定字符串