Advertisement
Guest User

Untitled

a guest
Oct 28th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* Remove from string s1 chars that are also in string s2 */
  5.  
  6. int tostrngs (FILE*, char*, char*, int*);
  7. void prntstr (char*, int);
  8. int rmvstr (char*, char*, int*);
  9.  
  10.  
  11. int main(int argc, char *argv[]) {
  12.     FILE *input; //Input file
  13.     char *s1, *s2; //Strings
  14.     int len [2]; //Length of s1 and s2
  15.    
  16.     len [0] = 0; //Lengrth of s1
  17.     len [1] = 0; //Length of s2
  18.     s1 = malloc ( sizeof (char) );
  19.     s2 = malloc ( sizeof (char) );
  20.    
  21.     if ( ! (input = fopen ("Input.txt", "r") ) ) {
  22.         printf ("Error opening file");
  23.         return 1;
  24.     }
  25.    
  26.     tostrngs (input, s1, s2, len); //Read strings from input file.
  27.     fflush (input);
  28.     fclose (input); //Close input file
  29.  
  30.     printf ("Length of s1 %d\nLength of s2 %d\n", len[0], len[1]);
  31.        
  32.     prntstr (s1,  len[0]); //Print both strings
  33.     prntstr (s2, len[1]);
  34.    
  35.     //rmvstr (s1, s2, len); //Remove from s1 characters that are also in s2
  36.    
  37.     //prntstr (s1, len[0]); //Print new s1
  38.        
  39.     free (s1);
  40.     free (s2);
  41.    
  42.     return 0;
  43. }
  44.  
  45. int tostrngs (FILE* input, char* s1, char* s2, int* len ) {
  46.     int i;
  47.     char c;
  48.    
  49.     for ( i = 0; (c = fgetc (input)) != '\n'; i++) { //Reading first string
  50.         len[0]++; //Increasing string length counter
  51.         s1 = realloc (s1, (len[0] + 1) * sizeof (char)); //Increasing allocated memory space
  52.         if (s1) { //Realllocation is successfull
  53.             s1 [i] = c; //Putting char from file to the string
  54.         } else {
  55.             free (s1);
  56.         }
  57.     }
  58.    
  59.     for ( i = 0; (c = fgetc (input) ) != EOF; i++ ) { //Reading second string
  60.         len[1]++;
  61.         s2 = realloc (s2, (len [1] + 1) * sizeof (char) );
  62.         if (s2) {
  63.             s2 [i] = c;
  64.         } else {
  65.             free (s2);
  66.         }
  67.     }
  68.    
  69.     return 0;
  70. }
  71.  
  72. void prntstr (char *str, int len) {
  73.     int i;
  74.    
  75.     for ( i = 0; i <= len; i++) {
  76.         printf ( "%c ", str [i] );
  77.     }
  78.     printf ("\n");
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement