WilliamIMS
asked on
Manipulating Arrays of Characters
Hi,
I would like to program the following in Delphi, but please only write algorithms as I would like to learn the programming language myself.
The following could held in an array of characters called 'names':
'Michael James Jones'
I would like it to be output in the format:
Jones,M.J.
There doesn't necessarily have to be a middle name but there will always be a forename and a surname.
Many thanks.
I would like to program the following in Delphi, but please only write algorithms as I would like to learn the programming language myself.
The following could held in an array of characters called 'names':
'Michael James Jones'
I would like it to be output in the format:
Jones,M.J.
There doesn't necessarily have to be a middle name but there will always be a forename and a surname.
Many thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ooo just re-read your question ... sorry if I gave too much info :o/
ASKER
Hi Mikelittlewood, if you could write an algorithm that uses a string that would be great.
Cheers.
Cheers.
usual a string is per definition an array of char which begins with the index 1
but to keep near on your question
function get_formated_name(CharArr : Array of Char) : String;
var
onFirst, getNext : boolean;
si,se,i : integer;
begin
result := '';
si := low(CharArr); //usual allways 0
se := high(CharArr);
onFirst := True;
getNext := false;
i := si;
while i < se do
begin
if (CharArr[i] <> ' ') and (onFirst) then
result := result + CharArr[i]
else
if (CharArr[i] = ' ') and (onFirst) then
begin
result := result + ',';
onFirst := False;
getNext := True;
end
else
if (CharArr[i] = ' ') and (not onFirst) then
getNext := True
else
if (CharArr[i] <> ' ') and (getNext) then
begin
result := result + CharArr[i]+'.';
getNext := False;
end;
inc(i);
end;
end;
just from head -> means not tested and typos possible
meikl ;-)
but to keep near on your question
function get_formated_name(CharArr : Array of Char) : String;
var
onFirst, getNext : boolean;
si,se,i : integer;
begin
result := '';
si := low(CharArr); //usual allways 0
se := high(CharArr);
onFirst := True;
getNext := false;
i := si;
while i < se do
begin
if (CharArr[i] <> ' ') and (onFirst) then
result := result + CharArr[i]
else
if (CharArr[i] = ' ') and (onFirst) then
begin
result := result + ',';
onFirst := False;
getNext := True;
end
else
if (CharArr[i] = ' ') and (not onFirst) then
getNext := True
else
if (CharArr[i] <> ' ') and (getNext) then
begin
result := result + CharArr[i]+'.';
getNext := False;
end;
inc(i);
end;
end;
just from head -> means not tested and typos possible
meikl ;-)
for a string:
just replace this first part:
function get_formated_name(CharArr : string) : String;
var
onFirst, getNext : boolean;
si,se,i : integer;
begin
result := '';
si := 1;
se := lengthCharArr);
....
but of course using pos and copy is more elegant
just from head -> means not tested and typos possible
meikl ;-)
just replace this first part:
function get_formated_name(CharArr : string) : String;
var
onFirst, getNext : boolean;
si,se,i : integer;
begin
result := '';
si := 1;
se := lengthCharArr);
....
but of course using pos and copy is more elegant
just from head -> means not tested and typos possible
meikl ;-)
kretzschmar your result will give Michael,J.J.
ASKER
Thank you all for your help but I'm trying not to look at delphi code, I want to use an algorithm!!
ok Im not sure what you need from me without me actually writing some code down to do it.
Why do you want to use an algorithm? there are much cleaner quicker methods you can use.
Why do you want to use an algorithm? there are much cleaner quicker methods you can use.
See mine only. It's just an algorithm :-)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ok here is my attempt at an algorithm description I guess.
This is along the lines of the one geobul gave I think, and Im sure he is describing a string checking one.
1) Have a function that passes in a string parameter (sName) and returns a string
2) Create 2 string variables, 1 for the initials (sInitials), 1 for the surname (sSurname)
3) Get the first character of the string and save as the initial variable with a '.' after it.
4) Get the index position of the first space and copy from index+1 to the end of the end of the string to strip off the first word.
5) Check if another space exists in the string. If a space exists then repeat steps 3 and 4 but add the character and '.' onto the previous sInitials variable.
6) Then all you are left with is the surname.
This is along the lines of the one geobul gave I think, and Im sure he is describing a string checking one.
1) Have a function that passes in a string parameter (sName) and returns a string
2) Create 2 string variables, 1 for the initials (sInitials), 1 for the surname (sSurname)
3) Get the first character of the string and save as the initial variable with a '.' after it.
4) Get the index position of the first space and copy from index+1 to the end of the end of the string to strip off the first word.
5) Check if another space exists in the string. If a space exists then repeat steps 3 and 4 but add the character and '.' onto the previous sInitials variable.
6) Then all you are left with is the surname.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What I gave is just an algorithm. Do not think for operators as they are in Delphi. There is no difference in the algorithm what datatypes will be used - string or array of char or something else. Actually Delphi string and array of char types are assignment compatible (with explicit typecasting):
StringVar := string(ArrOfCharVar);
And my algorithm will work with more than one middle name ;-)
I can post all the code immediately because I already have it. Or the necessary functions, at least, like 'Getting the last space occurance', 'copying a part of array somewhere else', 'deleting a part of array', etc.
Regards, Geo
StringVar := string(ArrOfCharVar);
And my algorithm will work with more than one middle name ;-)
I can post all the code immediately because I already have it. Or the necessary functions, at least, like 'Getting the last space occurance', 'copying a part of array somewhere else', 'deleting a part of array', etc.
Regards, Geo
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you would like an example using a string I can.