C语言字符串去掉指定字符


 

一、去掉字符串指定字符

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void del_char(char a[],char c)
 5 {
 6     int i,j;
 7     for(i=0,j=0; *(a+i)!='\0'; i++)
 8     {
 9         if(*(a+i)==c)
10             continue;
11         else
12         {
13             *(a+j)=*(a+i);
14             j++;
15         }
16     }
17     *(a+j)='\0';
18 }
19 int main()
20 {
21     char a[100],c;
22     scanf("%s %c",a,&c);
23     del_char(a,c);
24     printf("%s",a);
25     return 0;
26 }

运行结果:

 

 

注:

如果想去掉字符串最后一个字符,简便方法是:str[strlen( str ) - 1] = '\0';

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2026 CODEPRJ.COM