954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Challenging C Program Question

I need the coding for the following question in C:
Read and print all the consonants from a file (no duplicates). Assume the file is a .txt file. The consonants are then to be sorted in alphabetical order.

junezy4
Newbie Poster
3 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 
I need the coding for the following question in C: Read and print all the consonants from a file (no duplicates). Assume the file is a .txt file. The consonants are then to be sorted in alphabetical order.

Again, read this http://www.daniweb.com/forums/announcement118-2.html

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

The solution is pretty easy if you think about it. Every character in the English alphabet, and other characters too, have a numeric value between 0 and 255. To see what they are just google for "ascii chart". Given that, create an int array of 255 elements and initialize all of them to 0. Then read the file one character at a time, using the character as the index into the array. For example

int array[255] = {0};

array['A']++;

In the above the 65th element of the array will be incremented because the decimal value for the letter 'A' is 65. Instead of hardcoding the letter 'A' you would use the variable that you used to read the character in the file.

When you are done reading the file, all the consonants will already be in sorted order, so all you have to do is print them out. Loop through the array looking for array elements that are greater than 0 and are consonants.

Program done.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: