Friday, April 8, 2011

#C_CODE: squeeze(s,x) - Removes all occurances of character x from string s

/* Include files */
#include <stdio.h>
#include <string.h>
 

void squeeze(char *s,char x)
{
 int i,j;

 for(i=j=0; s[i] != '\0'; i++)
  if(s[i] != x)
   s[j++] = s[i];
 s[j] = '\0';
}

int main()
{
 char buff[35];

 fputs("Input string: ",stdout);
 fgets(buff,sizeof(buff),stdin);
 fflush(stdin);
 *(buff + strlen(buff) - 1) = '\0';

 squeeze(buff,'a');
 printf("The output: %s\n",buff);
 getchar();
}


Ref: C Programming Language - K&R

No comments: