Breaking strings apart? 
Author Message
 Breaking strings apart?

If I have a string name_all  with the value:"JohnHenrySmith" What is the
best way to break it into Name_first,Name_middle and Name_last???
Thank you for any suggestions.

--



Tue, 26 Mar 2002 03:00:00 GMT  
 Breaking strings apart?

Quote:

> If I have a string name_all  with the value:"JohnHenrySmith" What is the
> best way to break it into Name_first,Name_middle and Name_last???

This seems like a very artificial problem, since nobody is likely
to store names in that format.  But simply specifying in detail
what the required functionality is, practically dictates the code.
        copy from beginning to next capital letter to first_name
                array; add null terminator.
        etc.
--



Tue, 26 Mar 2002 03:00:00 GMT  
 Breaking strings apart?

Quote:

> If I have a string name_all  with the value:"JohnHenrySmith" What is the
> best way to break it into Name_first,Name_middle and Name_last???
> Thank you for any suggestions.

> --


Gunther,

If the parts of the names are truly concatenated together as you
show, then you are probably going to be forced to load a 'dictionary'
of Names (first, middle and last), and compare each dictionary
entry against your string.  I would look for a First name at the
beginning of the string, a Last name at the end of the string, then
finally a Middle name from what is left.  It will probably be an
iterative process, fraught with potential error.

Yours,

Geoff Houck
systems hk

http://www.teleport.com/~hksys
--



Tue, 26 Mar 2002 03:00:00 GMT  
 Breaking strings apart?
: If I have a string name_all  with the value:"JohnHenrySmith" What is the
: best way to break it into Name_first,Name_middle and Name_last???
: Thank you for any suggestions.

I would do something like this:

#include <ctype.h>

char* name="JohnHenrySmith";
int i;
for(i = 1; *(name  + i); ++i) {
  if(name[i] == toupper(name[i]) break;

Quote:
}

then allocate i bytes to Name_first, fill it with the first i-1 characters
of name followed by a null character, set name = name + i,
and repeat the procedure.  You probably want to check if(name[i])
between for statements.

HTH,
Eric

--

 Eindhoven Univ. of Technology
 Lab. for Catalysis and Inorg. Chem. (SKA)
--



Tue, 26 Mar 2002 03:00:00 GMT  
 Breaking strings apart?


Quote:
>If I have a string name_all  with the value:"JohnHenrySmith" What is the
>best way to break it into Name_first,Name_middle and Name_last???
>Thank you for any suggestions.

If there are delimeters of some kind in your name_all string, you
could use strtok(). Note that strtok will alter the original string
and it is best if you copy the string returned by strtok() to your
destination.

If there are no delimeters, you must split the string 'by hand'.
One way would be to make a sort of reverse_strtok().
Go over the string from the end to the start. Once you  find a
character that can start a token (e.g. a capital letter), you copy the
string that starts at the current position to the appropriate
variable. Then you put a '\0' at the current pusition to chop off the
part you have processed, and you continue scanning the string.

Quote:

>--


Bart v Ingen Schenau
--



Tue, 26 Mar 2002 03:00:00 GMT  
 Breaking strings apart?


Quote:
>If I have a string name_all  with the value:"JohnHenrySmith" What is the
>best way to break it into Name_first,Name_middle and Name_last???
>Thank you for any suggestions.

Try strpbrk() with the second argument listing the uppercase alphabet.
However it will fail when faced with  Celtic names such as JohnMcBride.

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Tue, 26 Mar 2002 03:00:00 GMT  
 Breaking strings apart?

Quote:

>> If I have a string name_all  with the value:"JohnHenrySmith" What is the
>> best way to break it into Name_first,Name_middle and Name_last???
> This seems like a very artificial problem, since nobody is likely
> to store names in that format.  But simply specifying in detail
> what the required functionality is, practically dictates the code.
>    copy from beginning to next capital letter to first_name
>            array; add null terminator.
>    etc.

For extra points, Make it work for my name.  :-)

Or, MichelDeLaRoche
or MicheledelaRoche

--

http://www.druid.net/darcy/                |  and a sheep voting on        
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.
--



Fri, 29 Mar 2002 03:00:00 GMT  
 Breaking strings apart?

Quote:

> If I have a string name_all  with the value:"JohnHenrySmith" What is the
> best way to break it into Name_first,Name_middle and Name_last???

Use a for loop and copy each character one at a time. Use isupper() to
detect a new part of a word.

How would you cope with "BrianMcCarthy"?

BillGodfrey
--



Fri, 29 Mar 2002 03:00:00 GMT  
 Breaking strings apart?
Thank you one and all for the time you took to help me.
It is really appreciated and I learned from it...
Gunther

Quote:
> If I have a string name_all  with the value:"JohnHenrySmith" What is the
> best way to break it into Name_first,Name_middle and Name_last???
> Thank you for any suggestions.

> --


--



Fri, 29 Mar 2002 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. How to break strings apart?

2. lint on Altos 2000 is BROKE BROKE BROKE!

3. RIP this function apart!

4. Yet another recursive file find routine (non MFC) - feel free to rip it apart

5. Telling members of same class apart while debugging...

6. Breaking up a string

7. breaking up string into array elements

8. Best way to break up a string?

9. C Breaking a string into substrings

10. break out pairs of data from a string.

11. breaking long string entries

12. Broken code or broken compiler?

 

 
Powered by phpBB® Forum Software