首页 > 技术文章 > C语言字符串去掉指定字符

yinguojin 2020-02-25 19:31 原文

 

一、去掉字符串指定字符

 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';

 

推荐阅读