Connecting Tech Pros Worldwide Forums | Help | Site Map

checking to see if a string contains every letter of the alphabet

booksnore
Guest
 
Posts: n/a
#1: Nov 17 '05
I am writing some code to search for strings that contain every letter
of the alphabet. At the moment I am using the method below to check to
see if a string contains every letter of the alphabet. I wanted to use a
regular expression but I could not find an ‘AND’ operator within the
regular expression – so I need something like match true if string
contains ‘a’ and ‘b’ and ‘c’ and ‘d’ ..etc,etc. If anyone has any
thoughts on how I can provide the check more effectively than I am doing
at the moment the help would be very much appreciated.


private static bool InAlphabet(string text)

{

if(text==null)

{

return false;

}



int count = 97;

bool IsValid = true;

while (IsValid && count < 123)

{

// Cast character from integral type

string regexStr = "(" + (char)count + ")";

Regex regex_x = new Regex(regexStr);

Match m_x = regex_x.Match(text);

IsValid = m_x.Success;

count++;

}
return IsValid;

}



*** Sent via Developersdex http://www.developersdex.com ***

Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#2: Nov 17 '05

re: checking to see if a string contains every letter of the alphabet


Hi,

I would do this, I will create a Hash with the letters (char) as key and
value 0 as values.
I will iterate in the chars of the string and set the value
somethins like this:

Hashtable hash = new Hashtable();

hash['a']=0;
hash['b']=0;
hash['c']=0;
....

foreach( char c in string.ToCharArray() )
hash[ c ] = 1;

foreach( int i in hash.Values )
if ( 0 == i )
return false;

return true;


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"booksnore" <booksnore@netscape.net> wrote in message
news:eGHfYbypFHA.1464@TK2MSFTNGP14.phx.gbl...[color=blue]
>I am writing some code to search for strings that contain every letter
> of the alphabet. At the moment I am using the method below to check to
> see if a string contains every letter of the alphabet. I wanted to use a
> regular expression but I could not find an 'AND' operator within the
> regular expression - so I need something like match true if string
> contains 'a' and 'b' and 'c' and 'd' ..etc,etc. If anyone has any
> thoughts on how I can provide the check more effectively than I am doing
> at the moment the help would be very much appreciated.
>
>
> private static bool InAlphabet(string text)
>
> {
>
> if(text==null)
>
> {
>
> return false;
>
> }
>
>
>
> int count = 97;
>
> bool IsValid = true;
>
> while (IsValid && count < 123)
>
> {
>
> // Cast character from integral type
>
> string regexStr = "(" + (char)count + ")";
>
> Regex regex_x = new Regex(regexStr);
>
> Match m_x = regex_x.Match(text);
>
> IsValid = m_x.Success;
>
> count++;
>
> }
> return IsValid;
>
> }
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***[/color]


KH
Guest
 
Posts: n/a
#3: Nov 17 '05

re: checking to see if a string contains every letter of the alphabet


Using a bitmap might be more efficient than allocating a hash table ...


const int CC_A = (int)'A'; // 65
const int CC_Z = (int)'Z'; // 90
const uint ONE = 0x01;
const uint MAX = 0xFFFFFFFF; // UInt32.MaxValue

string str = "abcdefghijklmnopqrstuvwxyz";

uint map = 4227858432; // first 26 bits OFF

int cc = 0; // char code

foreach (char ch in str)
{
cc = (int)Char.ToUpper(ch);

if (cc >= CC_A && cc <= CC_Z)
{
// In one line ...
// map |= (ONE << (cc - CC_A));

// Step by step...

// Subtract 65 from the char code so the mask fits in 32 bits
//
cc -= CC_A;

// Create the mask by shifting 1 bit the appropriate number of places
//
uint mask = ONE << cc;

// Reassign the bitmap
//
map |= mask;
}
}

if (map == MAX)
{
// TRUE, string contains all 26 alphabet chars
}



"Ignacio Machin ( .NET/ C# MVP )" wrote:
[color=blue]
> Hi,
>
> I would do this, I will create a Hash with the letters (char) as key and
> value 0 as values.
> I will iterate in the chars of the string and set the value
> somethins like this:
>
> Hashtable hash = new Hashtable();
>
> hash['a']=0;
> hash['b']=0;
> hash['c']=0;
> ....
>
> foreach( char c in string.ToCharArray() )
> hash[ c ] = 1;
>
> foreach( int i in hash.Values )
> if ( 0 == i )
> return false;
>
> return true;
>
>
> cheers,
>
> --
> Ignacio Machin,
> ignacio.machin AT dot.state.fl.us
> Florida Department Of Transportation
>
>
> "booksnore" <booksnore@netscape.net> wrote in message
> news:eGHfYbypFHA.1464@TK2MSFTNGP14.phx.gbl...[color=green]
> >I am writing some code to search for strings that contain every letter
> > of the alphabet. At the moment I am using the method below to check to
> > see if a string contains every letter of the alphabet. I wanted to use a
> > regular expression but I could not find an 'AND' operator within the
> > regular expression - so I need something like match true if string
> > contains 'a' and 'b' and 'c' and 'd' ..etc,etc. If anyone has any
> > thoughts on how I can provide the check more effectively than I am doing
> > at the moment the help would be very much appreciated.
> >
> >
> > private static bool InAlphabet(string text)
> >
> > {
> >
> > if(text==null)
> >
> > {
> >
> > return false;
> >
> > }
> >
> >
> >
> > int count = 97;
> >
> > bool IsValid = true;
> >
> > while (IsValid && count < 123)
> >
> > {
> >
> > // Cast character from integral type
> >
> > string regexStr = "(" + (char)count + ")";
> >
> > Regex regex_x = new Regex(regexStr);
> >
> > Match m_x = regex_x.Match(text);
> >
> > IsValid = m_x.Success;
> >
> > count++;
> >
> > }
> > return IsValid;
> >
> > }
> >
> >
> >
> > *** Sent via Developersdex http://www.developersdex.com ***[/color]
>
>
>[/color]
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#4: Nov 17 '05

re: checking to see if a string contains every letter of the alphabet


KH <KH@discussions.microsoft.com> wrote:[color=blue]
> Using a bitmap might be more efficient than allocating a hash table ...[/color]

Indeed - not as easy to understand the code as using a boolean array,
however:

static bool ContainsAllLetters(string x)
{
bool[] letters = new bool[26];

int lettersFound=0;

foreach (char c in x)
{
int index = char.ToUpper(c)-'A';

if (index >= 0 &&
index < letters.Length &&
!letters[index])
{
letters[index]=true;
lettersFound++;
}
}

return (lettersFound==letters.Length);
}

Note that this won't spot characters which have accents etc - but at
least it doesn't fail if you include any numbers.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
KH
Guest
 
Posts: n/a
#5: Nov 17 '05

re: checking to see if a string contains every letter of the alphabet


Using a bitmap might be more efficient than allocating a hash table ...


const int CC_A = (int)'A'; // 65
const int CC_Z = (int)'Z'; // 90
const uint ONE = 0x01;
const uint MAX = 0xFFFFFFFF; // UInt32.MaxValue

string str = "abcdefghijklmnopqrstuvwxyz";

uint map = 4227858432; // first 26 bits OFF

int cc = 0; // char code

foreach (char ch in str)
{
cc = (int)Char.ToUpper(ch);

if (cc >= CC_A && cc <= CC_Z)
{
// In one line ...
// map |= (ONE << (cc - CC_A));

// Step by step...

// Subtract 65 from the char code so the mask fits in 32 bits
//
cc -= CC_A;

// Create the mask by shifting 1 bit the appropriate number of places
//
uint mask = ONE << cc;

// Reassign the bitmap
//
map |= mask;
}
}

if (map == MAX)
{
// TRUE, string contains all 26 alphabet chars
}



"Ignacio Machin ( .NET/ C# MVP )" wrote:
[color=blue]
> Hi,
>
> I would do this, I will create a Hash with the letters (char) as key and
> value 0 as values.
> I will iterate in the chars of the string and set the value
> somethins like this:
>
> Hashtable hash = new Hashtable();
>
> hash['a']=0;
> hash['b']=0;
> hash['c']=0;
> ....
>
> foreach( char c in string.ToCharArray() )
> hash[ c ] = 1;
>
> foreach( int i in hash.Values )
> if ( 0 == i )
> return false;
>
> return true;
>
>
> cheers,
>
> --
> Ignacio Machin,
> ignacio.machin AT dot.state.fl.us
> Florida Department Of Transportation
>
>
> "booksnore" <booksnore@netscape.net> wrote in message
> news:eGHfYbypFHA.1464@TK2MSFTNGP14.phx.gbl...[color=green]
> >I am writing some code to search for strings that contain every letter
> > of the alphabet. At the moment I am using the method below to check to
> > see if a string contains every letter of the alphabet. I wanted to use a
> > regular expression but I could not find an 'AND' operator within the
> > regular expression - so I need something like match true if string
> > contains 'a' and 'b' and 'c' and 'd' ..etc,etc. If anyone has any
> > thoughts on how I can provide the check more effectively than I am doing
> > at the moment the help would be very much appreciated.
> >
> >
> > private static bool InAlphabet(string text)
> >
> > {
> >
> > if(text==null)
> >
> > {
> >
> > return false;
> >
> > }
> >
> >
> >
> > int count = 97;
> >
> > bool IsValid = true;
> >
> > while (IsValid && count < 123)
> >
> > {
> >
> > // Cast character from integral type
> >
> > string regexStr = "(" + (char)count + ")";
> >
> > Regex regex_x = new Regex(regexStr);
> >
> > Match m_x = regex_x.Match(text);
> >
> > IsValid = m_x.Success;
> >
> > count++;
> >
> > }
> > return IsValid;
> >
> > }
> >
> >
> >
> > *** Sent via Developersdex http://www.developersdex.com ***[/color]
>
>
>[/color]
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#6: Nov 17 '05

re: checking to see if a string contains every letter of the alphabet


KH <KH@discussions.microsoft.com> wrote:[color=blue]
> Using a bitmap might be more efficient than allocating a hash table ...[/color]

Indeed - not as easy to understand the code as using a boolean array,
however:

static bool ContainsAllLetters(string x)
{
bool[] letters = new bool[26];

int lettersFound=0;

foreach (char c in x)
{
int index = char.ToUpper(c)-'A';

if (index >= 0 &&
index < letters.Length &&
!letters[index])
{
letters[index]=true;
lettersFound++;
}
}

return (lettersFound==letters.Length);
}

Note that this won't spot characters which have accents etc - but at
least it doesn't fail if you include any numbers.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
KH
Guest
 
Posts: n/a
#7: Nov 17 '05

re: checking to see if a string contains every letter of the alphabet


An improvement both of us missed ... each loop should probably test whether
our TRUE condition has been met so we don't continue iterating the string
unnecessarily.


"Jon Skeet [C# MVP]" wrote:
[color=blue]
> KH <KH@discussions.microsoft.com> wrote:[color=green]
> > Using a bitmap might be more efficient than allocating a hash table ...[/color]
>
> Indeed - not as easy to understand the code as using a boolean array,
> however:
>
> static bool ContainsAllLetters(string x)
> {
> bool[] letters = new bool[26];
>
> int lettersFound=0;
>
> foreach (char c in x)
> {
> int index = char.ToUpper(c)-'A';
>
> if (index >= 0 &&
> index < letters.Length &&
> !letters[index])
> {
> letters[index]=true;
> lettersFound++;
> }
> }
>
> return (lettersFound==letters.Length);
> }
>
> Note that this won't spot characters which have accents etc - but at
> least it doesn't fail if you include any numbers.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>[/color]
KH
Guest
 
Posts: n/a
#8: Nov 17 '05

re: checking to see if a string contains every letter of the alphabet


An improvement both of us missed ... each loop should probably test whether
our TRUE condition has been met so we don't continue iterating the string
unnecessarily.


"Jon Skeet [C# MVP]" wrote:
[color=blue]
> KH <KH@discussions.microsoft.com> wrote:[color=green]
> > Using a bitmap might be more efficient than allocating a hash table ...[/color]
>
> Indeed - not as easy to understand the code as using a boolean array,
> however:
>
> static bool ContainsAllLetters(string x)
> {
> bool[] letters = new bool[26];
>
> int lettersFound=0;
>
> foreach (char c in x)
> {
> int index = char.ToUpper(c)-'A';
>
> if (index >= 0 &&
> index < letters.Length &&
> !letters[index])
> {
> letters[index]=true;
> lettersFound++;
> }
> }
>
> return (lettersFound==letters.Length);
> }
>
> Note that this won't spot characters which have accents etc - but at
> least it doesn't fail if you include any numbers.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>[/color]
booksnore
Guest
 
Posts: n/a
#9: Nov 17 '05

re: checking to see if a string contains every letter of the alphabet


Thank you for the replies, they are extremely helpful.


*** Sent via Developersdex http://www.developersdex.com ***
booksnore
Guest
 
Posts: n/a
#10: Nov 17 '05

re: checking to see if a string contains every letter of the alphabet


Thank you for the replies, they are extremely helpful.


*** Sent via Developersdex http://www.developersdex.com ***
Closed Thread