Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /* Remove from string s1 chars that are also in string s2 */
- int tostrngs (FILE*, char*, char*, int*);
- void prntstr (char*, int);
- int rmvstr (char*, char*, int*);
- int main(int argc, char *argv[]) {
- FILE *input; //Input file
- char *s1, *s2; //Strings
- int len [2]; //Length of s1 and s2
- len [0] = 0; //Lengrth of s1
- len [1] = 0; //Length of s2
- s1 = malloc ( sizeof (char) );
- s2 = malloc ( sizeof (char) );
- if ( ! (input = fopen ("Input.txt", "r") ) ) {
- printf ("Error opening file");
- return 1;
- }
- tostrngs (input, s1, s2, len); //Read strings from input file.
- fflush (input);
- fclose (input); //Close input file
- printf ("Length of s1 %d\nLength of s2 %d\n", len[0], len[1]);
- prntstr (s1, len[0]); //Print both strings
- prntstr (s2, len[1]);
- //rmvstr (s1, s2, len); //Remove from s1 characters that are also in s2
- //prntstr (s1, len[0]); //Print new s1
- free (s1);
- free (s2);
- return 0;
- }
- int tostrngs (FILE* input, char* s1, char* s2, int* len ) {
- int i;
- char c;
- for ( i = 0; (c = fgetc (input)) != '\n'; i++) { //Reading first string
- len[0]++; //Increasing string length counter
- s1 = realloc (s1, (len[0] + 1) * sizeof (char)); //Increasing allocated memory space
- if (s1) { //Realllocation is successfull
- s1 [i] = c; //Putting char from file to the string
- } else {
- free (s1);
- }
- }
- for ( i = 0; (c = fgetc (input) ) != EOF; i++ ) { //Reading second string
- len[1]++;
- s2 = realloc (s2, (len [1] + 1) * sizeof (char) );
- if (s2) {
- s2 [i] = c;
- } else {
- free (s2);
- }
- }
- return 0;
- }
- void prntstr (char *str, int len) {
- int i;
- for ( i = 0; i <= len; i++) {
- printf ( "%c ", str [i] );
- }
- printf ("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement