* How to check if letters are in a string?
@ 2015-07-18 9:00 Trish Cayetano
2015-07-18 10:23 ` Nasser M. Abbasi
2015-07-18 11:36 ` Björn Lundin
0 siblings, 2 replies; 6+ messages in thread
From: Trish Cayetano @ 2015-07-18 9:00 UTC (permalink / raw)
Hi,
How do you check if (exact number of) letters are in a string?
1. This example should PASS because every letter is found in the string
LETTERS: HID
STRING: HIDDEN
2. This example should FAIL because the letters contain 2 N's but the string only has 1 N.
LETTERS: NINE
STRING: HIDDEN
Pangram doesn't seem to be a solution... because it considers #2 above as PASS.
This is because pangrams check for AT LEAST one letter (this means, it considers a pangram even if the letter is a duplicate)
Is there another way how to check if (exact number of) letters are in a string?
Thank you very much!
====
Here is the sample code:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Maps; use Ada.Strings.Maps;
with Ada.Characters.Handling; use Ada.Characters.Handling;
procedure main is
function ispangram(txt: String) return Boolean is
lowtxt : String := To_Lower(txt);
letset,txtset : Character_Set;
begin
letset := To_Set("nine");
txtset := To_Set(lowtxt);
return (letset-txtset)=Null_Set;
end ispangram;
begin
put_line(Boolean'Image(ispangram("hidden")));
end main;
============================================
OUTPUT:
C:\Users\a0284014\Desktop\ada\pangram\obj\main
TRUE
[2015-07-18 16:52:42] process terminated successfully, elapsed time: 00.37s
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: How to check if letters are in a string?
2015-07-18 9:00 How to check if letters are in a string? Trish Cayetano
@ 2015-07-18 10:23 ` Nasser M. Abbasi
2015-07-18 10:52 ` Trish Cayetano
2015-07-18 11:07 ` Simon Clubley
2015-07-18 11:36 ` Björn Lundin
1 sibling, 2 replies; 6+ messages in thread
From: Nasser M. Abbasi @ 2015-07-18 10:23 UTC (permalink / raw)
On 7/18/2015 4:00 AM, Trish Cayetano wrote:
> Hi,
>
> How do you check if (exact number of) letters are in a string?
>
> 1. This example should PASS because every letter is found in the string
> LETTERS: HID
> STRING: HIDDEN
>
I do not follow. The 'D' in 'letters' occur 1 time but it occurs
2 times in 'string'. So this is not the _exact number_ of
letters in the string.
> 2. This example should FAIL because the letters contain 2 N's but the string only has 1 N.
> LETTERS: NINE
> STRING: HIDDEN
>
So why now this fails when 'letters' has more N's than in string, but
the first case did not fail when 'letters' had less D's than in string?
I think your problem specifications is not clear.
--Nasser
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: How to check if letters are in a string?
2015-07-18 10:23 ` Nasser M. Abbasi
@ 2015-07-18 10:52 ` Trish Cayetano
2015-07-18 11:07 ` Simon Clubley
1 sibling, 0 replies; 6+ messages in thread
From: Trish Cayetano @ 2015-07-18 10:52 UTC (permalink / raw)
On Saturday, July 18, 2015 at 6:23:56 PM UTC+8, Nasser M. Abbasi wrote:
> On 7/18/2015 4:00 AM, Trish Cayetano wrote:
> > Hi,
> >
> > How do you check if (exact number of) letters are in a string?
> >
> > 1. This example should PASS because every letter is found in the string
> > LETTERS: HID
> > STRING: HIDDEN
> >
>
> I do not follow. The 'D' in 'letters' occur 1 time but it occurs
> 2 times in 'string'. So this is not the _exact number_ of
> letters in the string.
>
> > 2. This example should FAIL because the letters contain 2 N's but the string only has 1 N.
> > LETTERS: NINE
> > STRING: HIDDEN
> >
>
> So why now this fails when 'letters' has more N's than in string, but
> the first case did not fail when 'letters' had less D's than in string?
>
> I think your problem specifications is not clear.
>
> --Nasser
Hi Nasser,
Thanks... This is the concept of Text Twist Game if you know that game...
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: How to check if letters are in a string?
2015-07-18 10:23 ` Nasser M. Abbasi
2015-07-18 10:52 ` Trish Cayetano
@ 2015-07-18 11:07 ` Simon Clubley
1 sibling, 0 replies; 6+ messages in thread
From: Simon Clubley @ 2015-07-18 11:07 UTC (permalink / raw)
On 2015-07-18, Nasser M. Abbasi <nma@12000.org> wrote:
> On 7/18/2015 4:00 AM, Trish Cayetano wrote:
>> Hi,
>>
>> How do you check if (exact number of) letters are in a string?
>>
>> 1. This example should PASS because every letter is found in the string
>> LETTERS: HID
>> STRING: HIDDEN
>>
>
> I do not follow. The 'D' in 'letters' occur 1 time but it occurs
> 2 times in 'string'. So this is not the _exact number_ of
> letters in the string.
>
>> 2. This example should FAIL because the letters contain 2 N's but the string only has 1 N.
>> LETTERS: NINE
>> STRING: HIDDEN
>>
>
> So why now this fails when 'letters' has more N's than in string, but
> the first case did not fail when 'letters' had less D's than in string?
>
> I think your problem specifications is not clear.
>
I think the characters in "letters" are supposed to be a subset of the
characters in "string",
To the OP: a couple of ways immediately come to mind.
1) Maintain a seperate boolean array whose size is the size of "string".
Set the position to true when a letter matches and ignore that position
when checking for subsequent letters.
or
2) Assign "string" to a temporary string and remove the matching character
from each pass. You use that temporary string when searching for subsequent
letters.
I really hope I have not just done your homework for you. :-)
Simon.
--
Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP
Microsoft: Bringing you 1980s technology to a 21st century world
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: How to check if letters are in a string?
2015-07-18 9:00 How to check if letters are in a string? Trish Cayetano
2015-07-18 10:23 ` Nasser M. Abbasi
@ 2015-07-18 11:36 ` Björn Lundin
2015-07-18 12:48 ` Trish Cayetano
1 sibling, 1 reply; 6+ messages in thread
From: Björn Lundin @ 2015-07-18 11:36 UTC (permalink / raw)
Looks like I managed to mail this to the op instead of post it to c.l.a ...
On 2015-07-18 11:00, Trish Cayetano wrote:
> Is there another way how to check if (exact number of) letters are in
a string?
> Thank you very much!
declare
My_Letters: string := "HID"
My_String: string := "HIDDEN"
Is_In_String : boolean := False;
begin
for L in My_Letters'range loop
Is_In_String := False;
for S in My_String'range loop
if My_Letters(L) = My_String(S) then
Is_In_String := True;
My_String(S) := ' '; --set to space, so letter is not reused
exit; -- inner loop
end if;
end loop;
exit when not Is_In_String;
end loop;
Text_io.put_Line("Is_In_String = "& Is_In_String'Img);
end;
not compiled, not tested but the spirit of it
should do what you want
-- -- Björn
--
--
Björn
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: How to check if letters are in a string?
2015-07-18 11:36 ` Björn Lundin
@ 2015-07-18 12:48 ` Trish Cayetano
0 siblings, 0 replies; 6+ messages in thread
From: Trish Cayetano @ 2015-07-18 12:48 UTC (permalink / raw)
On Saturday, July 18, 2015 at 7:35:13 PM UTC+8, björn lundin wrote:
> Looks like I managed to mail this to the op instead of post it to c.l.a ...
>
>
> On 2015-07-18 11:00, Trish Cayetano wrote:
> > Is there another way how to check if (exact number of) letters are in
> a string?
> > Thank you very much!
>
> declare
> My_Letters: string := "HID"
> My_String: string := "HIDDEN"
> Is_In_String : boolean := False;
> begin
> for L in My_Letters'range loop
> Is_In_String := False;
> for S in My_String'range loop
> if My_Letters(L) = My_String(S) then
> Is_In_String := True;
> My_String(S) := ' '; --set to space, so letter is not reused
> exit; -- inner loop
> end if;
> end loop;
> exit when not Is_In_String;
> end loop;
> Text_io.put_Line("Is_In_String = "& Is_In_String'Img);
> end;
>
>
>
> not compiled, not tested but the spirit of it
> should do what you want
>
> -- -- Björn
>
> --
> --
> Björn
Thank you Nasser and Björn!
Björn nailed it!
You are one genius, Björn! It's working fine after a few tweaks (really minor like adding Ada and adding packages). Thank you so much!
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2015-07-18 12:48 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-18 9:00 How to check if letters are in a string? Trish Cayetano
2015-07-18 10:23 ` Nasser M. Abbasi
2015-07-18 10:52 ` Trish Cayetano
2015-07-18 11:07 ` Simon Clubley
2015-07-18 11:36 ` Björn Lundin
2015-07-18 12:48 ` Trish Cayetano
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox