有一个字符串,内有若干字符,输入一个字符,要求程序将字符串中该字符删去。

 1 #include<stdio.h>

 2 

 3 void main()  4 {  5     setvbuf(stdout,NULL,_IONBF,0);  6     char str[80],ch;  7     int i,j;  8 

 9     printf("Input the string:\n"); 10  gets(str); 11     printf("Input the char to be deleted:"); 12     scanf("%c",&ch); 13 

14     j=0; 15     for(i=0;str[i]!='\0';i++) 16         if(str[i] != ch) 17  { 18             str[j] = str[i]; 19             j++; 20  } 21     str[j]='\0'; 22 

23     printf("The string is changed to:\n"); 24  puts(str); 25 }

这个题目当然可以设两个数组,把不删除的字符一一赋给新数组。

但这里只用一个数组,只把不被删除的字符保留下来。

由于i总是大于或者等于j,因此最后保留下来的字符不会覆盖未被检测处理的字符。

你可能感兴趣的:(字符串)