Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define FLUSH while(getchar() != '\n')
void editor(FILE*); /*function prototype*/
void character(FILE*);
void lower(FILE*);
void upper(FILE*);
void encrypt(FILE*);
void decrypt(FILE*);
void main()
{
int ch;
FILE *option1=NULL;
FILE*option2=NULL;
FILE*option3=NULL;
FILE*option4=NULL;
FILE*option5=NULL;
FILE*option6=NULL;
int calls[7] = {0};
ch = 0;
while(ch != -1)
{
printf("\n\nPlease choose an option below by typing a number between 1 and 8.\n\n");
printf("1\tCreate text file\n\n");
printf("2\tCount Characters,words and sentences\n\n");
printf("3\tConvert to lowercase\n\n");
printf("4\tConvert to uppercase\n\n");
printf("5\tEncrypt text\n\n");
printf("6\tDecrypt text\n\n");
printf("7\tDisplay text file\n\n");
printf("8\tExit program\n\n");
scanf(" %d", &ch);
FLUSH;
if(ch > 0 && ch < 8) calls[ch-1]++;
switch(ch) {
case 1:
editor(option1);
break;
case 2:
character(option2);
break;
case 3:
lower(option3);
break;
case 4:
upper(option4);
break;
case 5:
encrypt(option5);
break;
case 6:
decrypt(option6);
break;
}
}
return;
}
void editor(FILE *fname)
{
char fileName[13];
char text[82];
printf("Enter the name of file to create: ");
fgets(fileName, sizeof(fileName), stdin);
fileName[strlen(fileName)-1] = '\0';
fname = fopen(fileName, "w");
if(fname == NULL)
{
printf("\nThe file cannot be opened.");
printf("\nPlease check that the file exists.");
exit(1);
}
printf("Enter the text to write to the file: ");
fgets(text, sizeof(text), stdin);
text[strlen(text)-1] = '\0';
fprintf(fname, "%s", text);
fclose(fname);
}
void character(FILE *fname)
{
char text[81];
char fileName[13];
int count,count1,i,j,k;
count=0,count1=0, i=0, j=0,k=0;
printf("\nPlease enter the name of the file you wish to open: ");
scanf("%s",&fileName);
fname=fopen(fileName, "r");
if (fname==NULL)
{
printf("\nThe file cannot be opened.");
printf("\nPlease check that the file exists.");
return;
}
while (fgets(text,81,fname) !=NULL)
printf("\n%s", text);
for (i=0; text[i]!='\0';++i)
{
count++;
}
printf("\n\nThe file you just opened: %s, has: %d characters, ", fileName,count);
int charNum = 0;
int numWords = 0; /* Number of words found in the string */
int inWord = 0; /* State: Either in or out of a word; 0 or 1 */
/* While we don't use up our whole array */
while (text[j] != '\0')
{
if ((text[j] == ' ') || (text[j] == '\t') ||
(text[j] == '\n'))
{
inWord = 0;
}
else if (inWord == 0)
{
++numWords;
inWord = 1;
}
++j;
}
printf("%d words, and ", numWords);
for (k=0; text[k] !='\0';++k)
{
if (text[k]=='.' || text[k]=='!' || text[k]=='?')
{
count1++;
}
}
printf("%d sentences.\n", count1);
}
void lower(FILE *fname)
{
FILE *lower;
char text[81];
char fileName[13];
int i;
printf("\nPlease enter the name of the file you wish to open: ");
scanf("%s",&fileName);
fname=fopen(fileName, "r");
if (fname==NULL)
{
printf("\nThe file cannot be opened.");
printf("\nPlease check that the file exists.");
return;
}
printf("\nWhat name would you like to call the new lowercase file: ");
scanf("%s",&fileName);
lower = fopen(fileName, "w");
fgets(text, 81, fname);
text[strlen(text)-1] = '\0';
//while (fgets(text, sizeof(fileName), fname) != NULL)
for(i = 0; i < strlen(text); i++)
{
text[i] = tolower(text[i]);
}
fprintf(lower, "%s\n",text);
fclose(fname);
fclose(lower);
}
void upper(FILE *fname)
{
FILE *upper;
char text[81];
char fileName[13];
int i;
printf("\nPlease enter the name of the file you wish to open: ");
scanf("%s",&fileName);
fname=fopen(fileName, "r");
if (fname==NULL)
{
printf("\nThe file cannot be opened.");
printf("\nPlease check that the file exists.");
return;
}
printf("\nWhat name would you like to call the new uppercase file: ");
scanf("%s",&fileName);
upper = fopen(fileName, "w");
fgets(text, 81, fname);
text[strlen(text)-1] = '\0';
for(i = 0; i <strlen(text); i++)
{
text[i] = toupper(text[i]);
}
fprintf(upper, "%s\n",text);
fclose(fname);
fclose(upper);
}
void encrypt(FILE *fname)
{
FILE*encrypt;
char fileName[13];
char text[82];
int i;
printf("Enter the name of file you wish to decrypt: ");
scanf("%s",&fileName);
fname = fopen(fileName, "r");
if(fname == NULL)
{
printf("\nThe file cannot be opened.");
printf("\nPlease check that the file exists.");
return;
}
printf("\nWhat name would you like to call the encrypted file: ");
scanf("%s",&fileName);
encrypt = fopen(fileName, "w");
fgets(text, 81, fname);
text[strlen(text)-1] = '\0';
for (i=0;text[i] !='\0';i++)
{
if(text[i] < 65 || (text[i] > 90 && text[i] < 97) || text[i] > 122 )
{
;
}
else
{
if (text[i] == 'A')
text[i]='z';
else
if (text[i] == 'a')
text[i]='Z';
else
if (text[i] == 'B')
text[i]='y';
else
if (text[i] == 'b')
text[i]='Y';
else
if (text[i] == 'C')
text[i]='x';
else
if (text[i] == 'c')
text[i]='X';
else
if (text[i] == 'D')
text[i]='w';
else
if (text[i] == 'd')
text[i]='W';
else
if (text[i] == 'E')
text[i]='v';
else
if (text[i] == 'e')
text[i]='V';
else
if (text[i] == 'F')
text[i]='u';
else
if (text[i] == 'f')
text[i]='U';
else
if (text[i] == 'G')
text[i]='t';
else
if (text[i] == 'g')
text[i]='T';
else
{
putc(text[i] + 3, encrypt);
}
}
}
fprintf(encrypt, "%s\n",text);
fclose(fname);
fclose(encrypt);
}
void decrypt(FILE *fname)
{
FILE*decrypt;
char fileName[13];
char text[82];
int i;
printf("Enter the name of file you wish to decrypt: ");
scanf("%s",&fileName);
fname = fopen(fileName, "r");
if(fname == NULL)
{
printf("\nThe file cannot be opened.");
printf("\nPlease check that the file exists.");
return;
}
printf("\nWhat name would you like to call the encrypted file: ");
scanf("%s",&fileName);
decrypt = fopen(fileName, "w");
fgets(text, 81, fname);
text[strlen(text)-1] = '\0';
for (i=0;text[i] !='\0';i++)
{
if(text[i] < 65 || (text[i] > 90 && text[i] < 97) || text[i] > 122 )
{
;
}
else
{
if (text[i] == 'z')
text[i]='A';
else
if (text[i] == 'Z')
text[i]='a';
else
if (text[i] == 'y')
text[i]='B';
else
if (text[i] == 'Y')
text[i]='b';
else
if (text[i] == 'x')
text[i]='C';
else
if (text[i] == 'X')
text[i]='c';
else
if (text[i] == 'w')
text[i]='D';
else
if (text[i] == 'W')
text[i]='d';
else
if (text[i] == 'v')
text[i]='E';
else
if (text[i] == 'V')
text[i]='e';
else
if (text[i] == 'u')
text[i]='F';
else
if (text[i] == 'U')
text[i]='f';
else
if (text[i] == 't')
text[i]='G';
else
if (text[i] == 'T')
text[i]='g';
else
{
putc(text[i] - 3, decrypt);
}
}
}
fprintf(decrypt, "%s\n",text);
fclose(fname);
fclose(decrypt);
}