C language to remove the specified character string

 

A, designated character string is removed

 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 }

operation result:

 

 

Note:

If you want to remove the last character string, is simple: str [strlen (str) - 1] = '\ 0';

 

Guess you like

Origin www.cnblogs.com/yinguojin/p/12363330.html