frequency analysis

P

Pete

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
 
E

Emmanuel Delahaye

Pete said:
<...> 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";
 
A

Allin Cottrell

Pete said:
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 = 'a' + i;

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

Emmanuel Delahaye

In 'comp.lang.c' said:
or is there another way to represent the alphabet characters.

char Alphabet[26];
int i;

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


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

Bertrand Mollinier Toublet

Allin said:
Pete said:
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 = 'a' + i;

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

Jeff

Allin Cottrell said:
Pete said:
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 = 'a' + i;

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


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'.
 
A

Allin Cottrell

Jeff said:
Pete said:
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 = 'a' + i;

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



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
 
R

Richard Heathfield

Pete said:
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;
}
 
B

Ben Pfaff

Emmanuel Delahaye said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top