I am doing a project in a coding class and we are writing in
I am doing a project in a coding class and we are writing in C. I have this code so far but I can\'t seem to get it to work. The project outcome is the first picture and below it is my code. Below that are my errors and warnings, can I get some help writing this? Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
int charCount = 0;
int lwrcaseCount = 0;
int uprcaseCount = 0;
int digitCount = 0;
int specialCount = 0;
int count[94] = {0};
char charSym[94]; //94 = 126 - 33 +1
void initialze() {
int i;
for (i=0; i<94; i++)
charSym[i] = i+\'!\';
}
void registerChar (char ch) {
charCount++;
if (islower(ch))
lwrcaseCount++;
else if (isupper(ch))
uprrcaseCount++;
else if (isdigit(ch))
digitCount++;
else if (ispunct(ch))
specialCount++;
count[ch - \'!\'] ++;
}
void sort() {
int i,j,temp;
char tempChar;
for (i=0; i<94-1; i++)
for (j=0; j<94-1-i; j++)
if (charCount[j] < charCount[j+1]) {
temp = charCount[j];
charCount[j] = charCount[j+1];
charCount[j+1] = temp;
tempChar = charSym[j];
charSym[j] = charSym[j+1];
charSym[j+1] = tempChar;
}
}
void printStat() {
int i, count = -1;
printf(\"%d charecters\ \",charcount);
printf(\"lowercase=%d and uppercase=%d and digit=%d and special=%d\ \",lwrcaseCount, uprcaseCount, digitCount, specialCount);
sort();
for (i=charCount; i>0; i--){
for (i = 0; i < 94; i++) {
if (count!=charCount[i]) {
count = charCount[i];
if (count)
printf(\"\ Charecters occuring %d times: \",count);
else
printf(\"\ Charecters not found in input: \");
}
printf(\"%c \",charSym[i]);
}
}
int main() {
char *str;
int i, l;
initialize();
str = //take input your way;
l = length(str);
for(i=0; i<l; i++)
registerChar(str[i]);
printStat();
return 0;
}
Project overview: Ever wonder what is in a file? Ever wonder how many times each character actually occurs in the file? Probably not most people never think of things like that. This program takes an input source and tells the user what characters that source contains. It does not worry about whitespace (spaces, tabs, newlines) but instead focuses on the printable characters. These include: 26 upper-case letters, A-Z and 26 lower-case letters, a and 10 digits, 0-9 32 special characters: \"#$S&\' C [V1 This program reads from standard input until end-of-file. Once it hits end-of-file, it tells the user what characters it saw, ordered from the most-occurring character down to the characters that it did not see. As an example, if the user entered the input below (shown in re the program generates the output shown in blue The Quick Brown Fox Jumps over the Lazy Old Dog 123456789012345 the quick brown fox jumps over the lazy old dog 105 characters lower case 67 and uppercase 9 and digit-15 and special 14 Characters occurring 8 times o Characters occurring 6 times e Characters occurring 4 times h r u d l t Characters occurring 3 times Characters occurring 2 times a c g i k m n p s v w x y z O 1 2 3 4 5 Characters occurring 1 times b f j q B D F J L Q T 0 6 7 8 9 Characters not found in the input A C E G H I K M N P R S U V W X Y Z What You Need To Do Create a directory project3 on your machine. In that directory, create a file named contents.c In contents. c, write the code needed to find out what characters exist in the input. Make sure that you: o Have a header block of comments that includes your name and a brief overview of the program. Read from standard input until you hit end-of-fil o Prints the expected output in a clear, legible format Hint #1: There are some good built-in character functions in ctype.h that make coding easier. Make sure you understand the functions isdigit and isupper and islower and ispunct. Hint #2: You can use an array or arrays to count the number of times you see each printable character Initialize each element to zero and increment a given location when you see that letter) To test your program with various inputs, you can redirect input so that it comes from a file instead of the keyboard. As an example, if you wanted to see what the contents of your contents.c file was, you would type the command /a.out contents .c (using a.exe instead of a.out in WindowsSolution
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
int len = 0;
int sp = 0;
int lwrcaseCount = 0;
int uprcaseCount = 0;
int digitCount = 0;
int specialCount = 0;
char data[200];
int frequency[95];
int sortFrequency[95][2];
//To sort based on frequency of characters
void sort()
{
int max, loc, temp, x, y;
//Loops till end - 1 position of the frequency array
for(x = 0; x <= 95 - 1; x++)
{
//Initializes the max to the x position of the frequency array assuming it as maximum
max = frequency[x];
//Initializes of loc to x assuming location x contains the maximum value
loc = x;
//Loops through x + 1 to length - 1 of frequency array
for(y = x + 1; y <= 95 -1; y++)
{
//If current position of the frequency of the array is greater than the max
//then update the max with the current value of frequency and update the loc
if(frequency[y] > max)
{
max = frequency[y];
loc = y;
}//end of if
}//end of for loop
//If loc is not x then swap
if(loc != x)
{
temp = frequency[x];
frequency[x] = frequency[loc];
frequency[loc] = temp;
}//end of if
//Store the location of the maximum value in the zero column position of row x
sortFrequency[x][0]= loc;
//Store the maximum value in the first column position of row x
sortFrequency[x][1] = max;
}//end of for loop
}//end of function
//To count frequency of each character
void frequencyCount()
{
int c, d;
//Loops till the length of the inputed data
for(c = 0; c < len; c++)
{
//Loops from 33 to 126 which is the ascii values of required characters range
for(d = 33; d <= 126; d++)
{
//If the data in the current position is equal to the acii value
if(data[c] == d)
//Frequency 0 position is equal to 33 ascii.
//So 33 is deducted
frequency[d-33]++;
}//end of for loop
}//end of for loop
}//End of function
//Initializes the frequency array to zero
void initialize()
{
int c;
for(c = 0; c < 95; c++)
frequency[c];
}
//Read the file which contains data
void readFile()
{
//File pointer created
FILE *fptr;
char ch;
//File opened in read mode
fptr = fopen(\"Data1.txt\", \"r\");
//If fptr is null show error message and terminate
if (fptr == NULL)
{
printf(\"Cannot open file \ \");
exit(0);
}//end of if
//read the first character from the file
ch = fgetc(fptr);
//Loops till end of file
while (ch != EOF)
{
//Stores the character in data array
data[len++] = ch;
//reads another character from the file
ch = fgetc(fptr);
}//end of while
//close file
fclose(fptr);
}//End of method
//Display the required information
void dispData()
{
int c;
//Displays the file contents
printf(\"\ Entered Data \ \");
for(c = 0; c < len; c++)
printf(\"%c\", data[c]);
printf(\"\ Total Characters: %d\", len - sp);
printf(\"\ Total Lowercase Character: %d\", lwrcaseCount);
printf(\"\ Total Uppercase Character: %d\", uprcaseCount);
printf(\"\ Total Digit: %d\", digitCount);
printf(\"\ Total Special Character: %d\", specialCount);
printf(\"\ Characters Available\ \");
for(c = 0; c < 95; c++)
{
if(sortFrequency[c][1] != 0)
printf(\"\ Character %c occurs %d Times \", sortFrequency[c][0]+33, sortFrequency[c][1]);
}//end of for
printf(\"\ Characters not available \ \");
for(c = 0; c < 95; c++)
{
if(sortFrequency[c][1] == 0)
printf(\"\ Character %c occurs %d Times \", sortFrequency[c][0]+33, sortFrequency[c][1]);
}//end of for
}//end of function
//Counts lowercase, uppercase, digits, special characters and total characters
void count()
{
int c;
for(c = 0; c < len; c++)
{
if(islower(data[c]))
lwrcaseCount++;
else if (isupper(data[c]))
uprcaseCount++;
else if (isdigit(data[c]))
digitCount++;
else if (ispunct(data[c]))
specialCount++;
else
sp++;
}//end of for
}//end of function
//Main function
int main()
{
//Calls the methods
initialize();
readFile();
count();
frequencyCount();
//dispData();
sort();
dispData();
return 0;
}
Output:
Entered Data
This is
123 (2)
1%
Total Characters: 14
Total Lowercase Character: 5
Total Uppercase Character: 1
Total Digit: 5
Total Special Character: 3
Characters Available
Character 1 occurs 2 Times
Character 2 occurs 2 Times
Character i occurs 2 Times
Character s occurs 2 Times
Character % occurs 1 Times
Character ( occurs 1 Times
Character ) occurs 1 Times
Character 3 occurs 1 Times
Character T occurs 1 Times
Character h occurs 1 Times
Characters not available
Character + occurs 0 Times
Character , occurs 0 Times
Character - occurs 0 Times
Character . occurs 0 Times
Character / occurs 0 Times
Character 0 occurs 0 Times
Character 1 occurs 0 Times
Character 2 occurs 0 Times
Character 3 occurs 0 Times
Character 4 occurs 0 Times
Character 5 occurs 0 Times
Character 6 occurs 0 Times
Character 7 occurs 0 Times
Character 8 occurs 0 Times
Character 9 occurs 0 Times
Character : occurs 0 Times
Character ; occurs 0 Times
Character < occurs 0 Times
Character = occurs 0 Times
Character > occurs 0 Times
Character ? occurs 0 Times
Character @ occurs 0 Times
Character A occurs 0 Times
Character B occurs 0 Times
Character C occurs 0 Times
Character D occurs 0 Times
Character E occurs 0 Times
Character F occurs 0 Times
Character G occurs 0 Times
Character H occurs 0 Times
Character I occurs 0 Times
Character J occurs 0 Times
Character K occurs 0 Times
Character L occurs 0 Times
Character M occurs 0 Times
Character N occurs 0 Times
Character O occurs 0 Times
Character P occurs 0 Times
Character Q occurs 0 Times
Character R occurs 0 Times
Character S occurs 0 Times
Character T occurs 0 Times
Character U occurs 0 Times
Character V occurs 0 Times
Character W occurs 0 Times
Character X occurs 0 Times
Character Y occurs 0 Times
Character Z occurs 0 Times
Character [ occurs 0 Times
Character \\ occurs 0 Times
Character ] occurs 0 Times
Character ^ occurs 0 Times
Character _ occurs 0 Times
Character ` occurs 0 Times
Character a occurs 0 Times
Character b occurs 0 Times
Character c occurs 0 Times
Character d occurs 0 Times
Character e occurs 0 Times
Character f occurs 0 Times
Character g occurs 0 Times
Character h occurs 0 Times
Character i occurs 0 Times
Character j occurs 0 Times
Character k occurs 0 Times
Character l occurs 0 Times
Character m occurs 0 Times
Character n occurs 0 Times
Character o occurs 0 Times
Character p occurs 0 Times
Character q occurs 0 Times
Character r occurs 0 Times
Character s occurs 0 Times
Character t occurs 0 Times
Character u occurs 0 Times
Character v occurs 0 Times
Character w occurs 0 Times
Character x occurs 0 Times
Character y occurs 0 Times
Character z occurs 0 Times
Character { occurs 0 Times
Character | occurs 0 Times
Character } occurs 0 Times
Character ~ occurs 0 Times







