0% found this document useful (0 votes)
47 views3 pages

function Prototype

This C program contains functions to count and sort the letters and digits in a text file. It defines functions to get the counts, print the unsorted and sorted counts, and swap values. The main function calls getCount to populate arrays with letter and digit counts, and then calls printUnsorted and printSort to output the counts before and after sorting.

Uploaded by

Arrafi Lee
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views3 pages

function Prototype

This C program contains functions to count and sort the letters and digits in a text file. It defines functions to get the counts, print the unsorted and sorted counts, and swap values. The main function calls getCount to populate arrays with letter and digit counts, and then calls printUnsorted and printSort to output the counts before and after sorting.

Uploaded by

Arrafi Lee
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <string.h>
#include <ctype.h>

//Function prototype
const char FILE_NAME[] = "homework06.txt";

void getCount(int letters[],int *,int [],int );


void printUnsorted(int letters[],int digits[]);
void printSort(int letters[],int digits[]);
void swap(int *, int *);

#define MAX 26
#define LIMIT 10

int main()
{
int letters[MAX]={0};
int spaces=0;
int digits[LIMIT]={0};
int special=0;

getCount(letters,&spaces,digits,&special);

printUnsorted( letters, digits);

printSort(letters, digits);
return 0;
}

///////////////////////////////////
void getCount(int letters[],int *spaces,int digits[],int *special)
{
char c;

FILE * fp=fopen("c:\\temp\\source.txt","r");

while(!feof(fp))
{
c = fgetc(fp);

if (c == '—')
{
continue;
}

if(isalpha(c))
{
c = toupper(c);
letters[(c)-65]++;
}
else if (isdigit(c))
{
digits[c-'0']++;
}
else
{
*special++;
}
}
fclose(fp);
}

///////////////////////////////
void printUnsorted(int letters[],int digits[])
{
int current,walker,i;

for(i=0;i<26;i++)
{
printf("%c : %3d ", 'A'+i, letters[i]);
if (i%7 == 0)
{
printf("\n");
}
}

for(i=0;i<10;i++)
{
printf("%c : %4d", 'A'+i, digits[i]);
if(i%7 ==0)
{
printf("\n");
}
}
}

///////////////////////////////
void printSort(int letters[],int digits[])
{
int current,walker,i;
for(current=0;current<MAX;current++)
{
for(walker=MAX-1;walker > current;walker--)
{
if(letters[walker] < letters[walker-1])
{
swap(&letters[walker],&letters[walker-1]);
}
}
}
for(current=0;current<LIMIT;current++)
{
for(walker=LIMIT-1;walker > current;walker--)
{
if(digits[walker] < digits[walker-1])
{
swap(&digits[walker],&digits[walker-1]);
}
}
}
for(i=0;i<26;i++)
{
printf("%c : %3d ", 'A'+i, letters[i]);
if (i%7 == 0)
{
printf("\n");
}
}

for(i=0;i<10;i++)
{
printf("%c : %4d", 'A'+i, digits[i]);
if(i%7 ==0)
{
printf("\n");
}
}
}

/////////////////////////
void swap(int *x, int *y)
{
int c=*x;
*x=*y;
*y=c;
}

You might also like