Assignment 10
A).
Write a program to prompt a user for a filename. Count: the number of characters in the file (letters or numbers) the number of words in the file the number of lines in the file Print out your results on the screen.
#include
#include
int main (void) {
FILE* fin; char filename[64]; int charcount = 0; int wordcount = 0; int linecount = 0; char c; char word[64]; char line[256];
printf("Enter the input file name\n"); scanf("%s", filename);
if ((fin = fopen(filename, "r")) == NULL)
{
printf("Error opening input file\n"); return 1;
}
while (!feof(fin))
{
c = fgetc(fin); if (isalnum(c))
{
charcount++;
}
} rewind(fin); while (!feof(fin)) { fscanf(fin, "%s ", word); wordcount++; } rewind(fin); while (!feof(fin)) { fgets(line, 256, fin); linecount++; } printf("Number of characters: %d\n"
"Number of words: %d\n"
"Number of lines: %d\n", charcount, wordcount, linecount); fclose(fin); return 0;
}
B).
Create a set/list of enumerated constants called week that contains the days of the week. Have a variable called today that is of type week. Assign a value to today. If the day is Monday through Friday, print "Go to work!" If the day is Saturday or Sunday, print "You can rest today!"
#include
#include
#include
#include
void main() { printf("Enter a day of the week! (i.e. Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,Sunday)\n"); enum week {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
enum week today; char todayString[16];
today = Sunday;
gets(todayString); if (stricmp(todayString,"Sunday") == 0) { today = Sunday; } if (stricmp(todayString,"Monday") == 0) { today = Monday; } if (stricmp(todayString,"Tuesday") == 0) { today = Tuesday; } if (stricmp(todayString,"Wednesday") == 0) { today = Wednesday; } if (stricmp(todayString,"Thursday") == 0) { today = Thursday; }