Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 29th, 2008, 08:48 PM
Registered User
 
Join Date: Aug 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default data structure for storing ascii inputs

I'm trying to figure out how to write a program in C that will take in characters, typed or specified by a file and count their occurences and print out how many each are.

I am not too clear how to do this. I am thinking of an array of char that index each ascii value and check each character agains the ascii array if there is increment the number of the ascii at that character index. But the next loop around wouldn't that change my ascii number and now i have nothing to check against when new inputs of the same character and ascii values are found.

Does anyone understand what I'm trying to explain? I have no programming level, in C. This is my introduction too it.

If anyone have a suggestion on how i can achieve this task please give me some advice.

Thanks ahead,
Ck

 
Old September 11th, 2008, 05:00 PM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default

A brute force way to do this is to define a 26 element integer array named count[] and initialize all elements to 0. Now suppose all of your character data is stored in a string named data[]. Code similar to this should work:

   char c;
   int count[26];
   int i = 0;
   int index;

   memset(&count, 0, sizeof(int) * 26); /* Init array to zero */

   while (data[i] != null) /* Look at each character */
   {
      c = toupper(data[i]); /* Make it uppercase */
      if (c >= 'A' && c <= 'Z') /* Is it an alpha character? */
      {
         index = (int) (c - 'A'); /* Make an index into the array */
         count[index]++; /* Increment its count */
      }
      i++; /* Look at next letter */
   }

While my C is rusty, this should give you an idea. For example, if the first letter in data[0] is 'd', the call to toupper() makes it 'D'. The next line checks to insure that it's a letter. The next line takes the letter D (which is ASCII code 68) and subtracts the letter 'A' (ASCII code 65) from it, which assigns 3 into index. The next statement increments that count in the array. A little thought will show:

   count[0] = 'A'
   count[1] = 'B'
   count[2] = 'C'
   count[3] = 'D'

and so on. If you work out a short sentence in the data[] array, you'll find you end up with a count of the number of occurrences of each letter.

Dr. Purdum







Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in e-mail structure sending data from data tiawebchd General .NET 3 May 5th, 2008 08:07 AM
data exporting into ascii and more amartya_mandal ASP.NET 1.0 and 1.1 Professional 0 June 8th, 2007 09:22 AM
inserting XML/ASCII data into SQL using VB.net outcast1881 Other Programming Languages 0 July 20th, 2006 07:39 AM
Need help with tree data structure vidhya_venkat C++ Programming 0 June 14th, 2006 01:13 PM
Reading ASCII data from text file. LordBeholder VB How-To 2 June 25th, 2004 05:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.