473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

frequency analysis

I need to write a program to do a frequency analysis on a string of text.
Firstly I need to get the alphabet A to Z for comparison but I'm not sure
how to go about this.

Is the following the only way to do it:

Alphabet[26] = {'a','b','c',...,'z'};

or is there another way to represent the alphabet characters.
Thankyou
Nov 13 '05 #1
8 6311
In 'comp.lang.c', "Pete" <No****@blank.com> wrote:
<...> I need to get the alphabet A to Z for comparison but I'm not sure
how to go about this.

Is the following the only way to do it:

Alphabet[26] = {'a','b','c',...,'z'};

or is there another way to represent the alphabet characters.


static char const Alphabet[26] = "abcdefghijklmnopqrstuvwxyz";

But if you want A to Z :

static char const Alphabet[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

Beware, in both cases, despite the appearences, 'Alphabet' is /not/ a string
literal. str*() functions won't work. If you want string literals, just
remove the size:

static char const Alphabet[] = "abcdefghijklmnopqrstuvwxyz";
or
static char const Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #2
Pete wrote:
I need to write a program to do a frequency analysis on a string of text.
Firstly I need to get the alphabet A to Z for comparison but I'm not sure
how to go about this.

Is the following the only way to do it:

Alphabet[26] = {'a','b','c',...,'z'};

or is there another way to represent the alphabet characters.


char Alphabet[26];
int i;

for (i=0; i<26; i++) Alphabet[i] = 'a' + i;

There are also isalpha(), islower() and isupper() to consider
(#include <ctype.h>).

--
Allin Cottrell
Department of Economics
Wake Forest University, NC

Nov 13 '05 #3
In 'comp.lang.c', Allin Cottrell <co******@wfu.edu> wrote:
or is there another way to represent the alphabet characters.


char Alphabet[26];
int i;

for (i=0; i<26; i++) Alphabet[i] = 'a' + i;


Wrong advice. The alphabetic character values don't have to be consecutive in
C.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #4
Allin Cottrell wrote:
Pete wrote:
I need to write a program to do a frequency analysis on a string of text.
Firstly I need to get the alphabet A to Z for comparison but I'm not sure
how to go about this.

Is the following the only way to do it:

Alphabet[26] = {'a','b','c',...,'z'};

or is there another way to represent the alphabet characters.

char Alphabet[26];
int i;

for (i=0; i<26; i++) Alphabet[i] = 'a' + i;

This is plain wrong. There is no guarantee whatsoever that alphabetic
characters are consecutive nor in ascending order.

--
Bertrand Mollinier Toublet
"Uno no se muere cuando debe, sino cuando puede"
-- Cor. Aureliano Buendia

Nov 13 '05 #5

"Allin Cottrell" <co******@wfu.edu> wrote in message
news:bg***********@f1n1.spenet.wfu.edu...
Pete wrote:
I need to write a program to do a frequency analysis on a string of text. Firstly I need to get the alphabet A to Z for comparison but I'm not sure how to go about this.

Is the following the only way to do it:

Alphabet[26] = {'a','b','c',...,'z'};

or is there another way to represent the alphabet characters.


char Alphabet[26];
int i;

for (i=0; i<26; i++) Alphabet[i] = 'a' + i;

There are also isalpha(), islower() and isupper() to consider
(#include <ctype.h>).

--
Allin Cottrell
Department of Economics
Wake Forest University, NC


The C standard said

5.2.1 Character sets
"The values of the members of the execution character set are
implementation-defined."

Your teacher may told you that we can increase the 'A' char(65) to get 'B'
char(66), but it is not always correct. The characters defined by ASCII is
consecutive, but in C, it is 'implementation-defined'.

--
Jeff
Nov 13 '05 #6
Jeff wrote:
"Allin Cottrell" <co******@wfu.edu> wrote in message
news:bg***********@f1n1.spenet.wfu.edu...
Pete wrote:
I need to write a program to do a frequency analysis on a string of
text.
Firstly I need to get the alphabet A to Z for comparison but I'm not
sure
how to go about this.

Is the following the only way to do it:

Alphabet[26] = {'a','b','c',...,'z'};

or is there another way to represent the alphabet characters.


char Alphabet[26];
int i;

for (i=0; i<26; i++) Alphabet[i] = 'a' + i;

There are also isalpha(), islower() and isupper() to consider
(#include <ctype.h>).

--
Allin Cottrell
Department of Economics
Wake Forest University, NC

The C standard said

5.2.1 Character sets
"The values of the members of the execution character set are
implementation-defined."

Your teacher may told you that we can increase the 'A' char(65) to get 'B'
char(66), but it is not always correct. The characters defined by ASCII is
consecutive, but in C, it is 'implementation-defined'.


You're right. I recalled that the standard guarantees that the
digits '0' through '9' have consecutive numerical values and incorrectly
generalized this to the Latin alphabet.

Allin Cottrell

Nov 13 '05 #7
Pete wrote:
I need to write a program to do a frequency analysis on a string of text.
Firstly I need to get the alphabet A to Z for comparison but I'm not sure
how to go about this.

Is the following the only way to do it:

Alphabet[26] = {'a','b','c',...,'z'};

or is there another way to represent the alphabet characters.


static const char lower[] = "abcdefghijklmnopqrstuvwxyz";
static const char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

But why restrict yourself to letters?

Here's a little frequency counter that counts /everything/, provided
UCHAR_MAX isn't too huge.

#include <limits.h>
#include <stdio.h>

int main(void)
{
unsigned long Count[UCHAR_MAX + 1] = {0};
int ch = 0;
while((ch = getchar()) != EOF)
{
++Count[ch];
}

/* do any output you wanted right here */

return 0;
}

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #8
Emmanuel Delahaye <em**********@noos.fr> writes:
But if you want A to Z :

static char const Alphabet[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

Beware, in both cases, despite the appearences, 'Alphabet' is /not/ a string
literal.

str*() functions won't work. If you want string literals, just
remove the size:

static char const Alphabet[] = "abcdefghijklmnopqrstuvwxyz";


This is an abuse of the term "string literal", which is defined
in C99 6.4.5#2:

A character string literal is a sequence of zero or more
multibyte characters enclosed in double-quotes, as in "xyz".

"abcdefghijklmnopqrstuvwxyz" is a string literal.
`Alphabet' is merely a string.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: christopher diggins | last post by:
I would like to survey how widespread the usage of smart pointers in C++ code is today. Any anecdotal experience about the frequency of usage of smart pointer for dynamic allocation in your own...
7
by: Dung Ping | last post by:
Such as: <script> //code aaaa zzzz ccc
1
by: John | last post by:
I can't find this in the manuals. I'm not interested in size of primary or secondary logs, what is a good rule of thumb for how many logs to cut in an hour in a busy OLTP and DW environment. I...
11
by: NC Tim | last post by:
Hello, I think the question i have is fairly straightforward, but I can't seem to replicate the old SAS frequency procedure when I try to accomplish this in MS Access. anyway, i have about 10...
19
by: jason_box | last post by:
I'm alittle new at C and I'm trying to write a simple program that will record the frequency of words and just print it out. It is suppose to take stdin and I heard it's only a few lines but I'm...
7
by: Udhay | last post by:
How to get the frequency of an audio file and how to separate the low and high frequency of an audio file
1
by: goldtech | last post by:
In Python 2.1 are there any tools to take a column from a DB and do a frequency analysis - a breakdown of the values for this column? Possibly a histogram or a table saying out of 500 records I...
8
by: Andrew Savige | last post by:
I'm learning Python by reading David Beazley's "Python Essential Reference" book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not...
13
by: umpsumps | last post by:
Hello, Here is my code for a letter frequency counter. It seems bloated to me and any suggestions of what would be a better way (keep in my mind I'm a beginner) would be greatly appreciated.. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.