So basically, in the code below I try to read the file names (i.e. input.txt and output.txt) from the keyboard but I get "segmentation fault". The program also converts the lower-case letters into upper-case letters and the upper-case letters into lower-case letters. Any suggestions? What am I doing wrong?
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char c;
int charToLowerCase = 0;
int charToUpperCase = 0;
int countCharacters = 0;
FILE *in_file = NULL;
FILE *out_file = NULL;
char str_in[100];
char str_out[100];
char *s_in = NULL;
char *s_out = NULL;
gets(str_in);
gets(str_out);
if (s_in != NULL)
in_file = fopen(str_in, "r");
if (s_out != NULL)
out_file = fopen(str_out, "w");
c = fgetc(in_file);
while (c != EOF)
{
if (c >= 'A' && c <= 'Z')
{
fprintf(out_file, "%c", tolower(c));
charToLowerCase++;
}
else if (c >= 'a' && c <= 'z')
{
fprintf(out_file, "%c", toupper(c));
charToUpperCase++;
}
else
fprintf(out_file, "%c", c);
c = fgetc(in_file);
countCharacters++;
}
fprintf(out_file, "\n");
fprintf(out_file, "Read %d characters in total, %d converted to upper-case, %d to lower-case.\n", countCharacters, charToUpperCase, charToLowerCase);
fclose(in_file);
fclose(out_file);
return 0;
}