lolamontes69

K+R Exercise2_4

Sep 5th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void squeeze(char s1[], char s2[]);
  4.  
  5. int main()
  6. {
  7.     int c, i, n;
  8.     char s1[255];
  9.     char s2[255];
  10.    
  11.     printf("Enter a string >");
  12.     i=0;
  13.     while((c=getchar())!=EOF && c!='\n') {
  14.         s1[i]=c;
  15.         ++i;
  16.     }                      
  17.     s1[i]='\0';      // doesn't automatically add '\0'
  18.  
  19.     n=0;
  20.     printf("Enter char to exclude >");
  21.     while((c=getchar())!=EOF && c!='\n') {
  22.         s2[n]=c;
  23.         ++n;
  24.     }                      
  25.     s2[n]='\0';      // doesn't automatically add '\0'
  26.     if(i==0) printf("\n<<<Try using data>>>\n");
  27.     else squeeze(s1, s2);
  28.     return(0);
  29. }
  30.  
  31. void squeeze(char s1[], char s2[])
  32. {
  33.     int a; // outer loop
  34.     int i, j; // inner loop
  35.  
  36.     for(a = 0; s2[a] != '\0'; a++) {
  37.         for(i = j = 0; s1[i] != '\0'; i++) {
  38.             if(s1[i] != s2[a])
  39.                 s1[j++] = s1[i];       //  inc after assignment
  40.         }
  41.         s1[j] = '\0';
  42.     }
  43.     for(i = j = 0; s1[i] != '\0'; i++)
  44.         printf("%c",s1[i]);
  45.     puts("\nValidated");
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment