I have a program which works with strings (Pascal). After reading a string if the first char is not a letter then I need to delete all first characters until the first is a letter. I have tried to write it several times, but always it deletes all string or nothing.
If program reads “123%^&abc” then result should be “abc”
In ASCII table letters are from 65..90 and from 97..122
This is how far I am:
variables a: set of 65..90;
b: set of 97..122;
-------------------
bool:=false;
While (bool=false) do
begin
Writeln(s[1]);
If (Ord(s[1]) in a) or (Ord(s[1]) in b) then
begin
bool:=true;
end else
delete(s,1,1);
end;
I don’t understand why it does not work?
Can you help me with this little procedure? Thank you.
You could do
or, as a procedure
For more sophisticated methods, that also work with Unicode Delphi, see my answer to a similar question. [This removes all non-alpha chars from the string.]
So, why doesn’t your algorithm work? Well, it should work, and it works for me. But notice that it can be written in the slightly more elegant form
One problem, however, with the OP’s original code is that it will fail if
sis the empty string. Indeed, thens[1]doesn’t exist. It won’t work either ifsconsists entirely of non-alpha characters (e.g.'!"#¤%).