A C function to capatalize first and last names. 
Author Message
 A C function to capatalize first and last names.

If there is a function in the C library that checks for capitalization in an
array of names, I DON'T want to know!

Rather, I would like to beat my head against a wall, do it myself, and
re-invent the wheel.

I am thinking about a loop that looks for the value of the array's address,
deceides it is lower case, and then subtracts a fixed value from the character,
if it is true(32?), since the difference between the little 'a' and the 'A'
etc.... are always the same.

Am I on the right path or wasting time with faulty logic?

I am not asking for you to do my homework or send code, but rather if I am on
the right track.

Thanks, Greg

--



Sat, 29 Jun 2002 03:00:00 GMT  
 A C function to capatalize first and last names.

Quote:

> If there is a function in the C library that checks for capitalization in an
> array of names, I DON'T want to know!

You're in luck.  There is no such function.

Quote:

> Rather, I would like to beat my head against a wall, do it myself, and
> re-invent the wheel.

> I am thinking about a loop that looks for the value of the array's address,

Depending on what this means, you may be right.
You want to look at each string, rather than at the array's address

  char *name[] = { /* list of quoted names */ };
  size_t name_cnt = sizeof names/sizeof *names,
         index;
  for (index = 0; index < name_cnt; index++)
  {
    /* name[index] is the string */

Quote:
> deceides it is lower case, and then subtracts a fixed value from the character,
> if it is true(32?), since the difference between the little 'a' and the 'A'
> etc.... are always the same.

Such as subtraction depends upon a particular encoding of the character
set.  Luckily it is not needed.  Nor is the test needed:

    *name[index] = tolower(*name[index]);

Quote:

> Am I on the right path or wasting time with faulty logic?

At the detail level, you are wrong, but the general approach is OK.

Quote:

> I am not asking for you to do my homework or send code, but rather if I am on
> the right track.

> Thanks, Greg

> --


--

What one knows is, in youth, of little moment; they know enough who
know how to learn. - Henry Adams

A thick skin is a gift from God. - Konrad Adenauer
__________________________________________________________
Fight spam now!
Get your free anti-spam service: http://www.brightmail.com
--



Sun, 30 Jun 2002 03:00:00 GMT  
 A C function to capatalize first and last names.


Quote:
>If there is a function in the C library that checks for capitalization in an
>array of names, I DON'T want to know!

>Rather, I would like to beat my head against a wall, do it myself, and
>re-invent the wheel.

>I am thinking about a loop that looks for the value of the array's address,
>deceides it is lower case, and then subtracts a fixed value from the character,
>if it is true(32?), since the difference between the little 'a' and the 'A'
>etc.... are always the same.

Not the best way. By array of names I assume that you mean something
like:

char *names[] = {"smith", "Jones", "Baker"}

If so then toupper() in ctype.h will do the job.

Quote:

>Am I on the right path or wasting time with faulty logic?

>I am not asking for you to do my homework or send code, but rather if I am on
>the right track.

>Thanks, Greg

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Bob Wightman
--



Sun, 30 Jun 2002 03:00:00 GMT  
 A C function to capatalize first and last names.


Quote:
>If there is a function in the C library that checks for capitalization in an
>array of names, I DON'T want to know!

>Rather, I would like to beat my head against a wall, do it myself, and
>re-invent the wheel.

>I am thinking about a loop that looks for the value of the array's address,
>deceides it is lower case, and then subtracts a fixed value from the character,
>if it is true(32?), since the difference between the little 'a' and the 'A'
>etc.... are always the same.

You are making assumptions about the character coding being used.  You
would be much better advised to use the standard functions islower and
toupper which will work correctly in any environment for which they were
designed.

Quote:

>Am I on the right path or wasting time with faulty logic?

>I am not asking for you to do my homework or send code, but rather if I am on
>the right track.

There is, of course, a further problem in that names can have unusual
upper/lower case usage.

Quote:

>Thanks, Greg

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
--



Sun, 30 Jun 2002 03:00:00 GMT  
 A C function to capatalize first and last names.

Quote:

> If there is a function in the C library that checks for capitalization in an
> array of names, I DON'T want to know!

Fortuantely for you, there isn't :)

Quote:

> Rather, I would like to beat my head against a wall, do it myself, and
> re-invent the wheel.

(I assume this is a homework problem and will treat it accordingly.)

Quote:

> I am thinking about a loop that looks for the value of the array's address,
> deceides it is lower case, and then subtracts a fixed value from the character,
> if it is true(32?), since the difference between the little 'a' and the 'A'
> etc.... are always the same.

This sounds fairly reasonable, except that it is not always true that the
difference between capitals and lowers is always the same (e.g. EBCDIC
computers).  Instead you should look at the macros defined in ctype.h,
specifically islower() and toupper().

You don't need to completely reinvent the wheel -- at least some of the
spokes have already been built for you.

(Also don't forget that the name may have more than one part which needs
to be capitalized.  e.g. "van houton" should become . . . um, bad example --
van probably shouldn't be capitalized -- how about "saint james" should
become "Saint James".  I expect that the prof doesn't expect you to deal
with van and de anyway, but you should handle multiple words and capitalize
all of them.)

/peter
--



Sun, 30 Jun 2002 03:00:00 GMT  
 A C function to capatalize first and last names.

Quote:

>I am thinking about a loop that looks for the value of the array's address,
>deceides it is lower case, and then subtracts a fixed value from the character,
>if it is true(32?),
>since the difference between the little 'a' and the 'A'
>etc.... are always the same.

Only true for ASCII, and English, neither of which are guaranteed by
C.  That's why toupper() exists.

Quote:
>Am I on the right path or wasting time with faulty logic?

Faulty logic.

Also remember there are some names like McDonald with multiple
capitalization.  It is easy to get it to work for most cases but there
will probably be names that screws your algorithm up.

Stephen
--



Sun, 30 Jun 2002 03:00:00 GMT  
 A C function to capatalize first and last names.

Quote:
>If there is a function in the C library that checks for capitalization in an
>array of names, I DON'T want to know!

The function I'm going to mention only handles one character at
a time, so perhaps you won't mind knowing about it.

Quote:
>Rather, I would like to beat my head against a wall, do it myself, and
>re-invent the wheel.

>I am thinking about a loop that looks for the value of the array's address,
>deceides it is lower case, and then subtracts a fixed value from the
>character,
>if it is true(32?), since the difference between the little 'a' and the 'A'
>etc.... are always the same.

>Am I on the right path or wasting time with faulty logic?

Take a look at the standard library function tolower().  This is a better
way than subtracting a fixed value.  It will be clearer what's going on,
and it will still work if the program is recompiled for a different
character set.

Are you trying to convert the names to all upper case, or trying to
capitalize them properly?  If the latter, don't forget about names like:

MacDonald
von Mises
O'Neal

There are more problems like these, so you should be able to beat
your head against a wall for a good long time.

-- Gary Culp
-- Keil Software
--



Sun, 30 Jun 2002 03:00:00 GMT  
 A C function to capatalize first and last names.


Quote:
>If there is a function in the C library that checks for capitalization in an
>array of names, I DON'T want to know!

>Rather, I would like to beat my head against a wall, do it myself, and
>re-invent the wheel.

>I am thinking about a loop that looks for the value of the array's address,
>deceides it is lower case, and then subtracts a fixed value from the character,
>if it is true(32?), since the difference between the little 'a' and the 'A'
>etc.... are always the same.

>Am I on the right path or wasting time with faulty logic?

Your logic is partly faulty. There is no guarantuee that letters are
adjacent in the translation table your computer uses to translate a
character to it's internal coding. There is also no guarantuee that
you can use some simple calculation to convert an uppercase letter to
lowercase and vice versa.

There are however functions that can tell if a character is actually a
letter, or if it is uppercase, etc, and there are functions that
convert uppercase to lowercase and vice versa.

check out isupper() and toupper().

Quote:

>I am not asking for you to do my homework or send code, but rather if I am on
>the right track.

>Thanks, Greg

>--


Bart v Ingen Schenau
--



Sun, 30 Jun 2002 03:00:00 GMT  
 A C function to capatalize first and last names.
  I think there are two answers to you question

1.  Yes, this assignment is very simple, with only two prerequisites:
  a)  English language
  b)  ASCII code: there are other encodings of English letters, where
          I.   'a'...'z' and 'A'...'Z' are not continuos sequences
         II.  ('Z'-'z') might be different from ('A'-'a')
       e.g. EBCDIC
2. If your names are not always written in English, or your computer is not ASCII, you'd better use
the NLS support of C.  For reference, just see what happens with capitalization in French or German.

  Michael

Quote:

> If there is a function in the C library that checks for capitalization in an
> array of names, I DON'T want to know!

> Rather, I would like to beat my head against a wall, do it myself, and
> re-invent the wheel.

> I am thinking about a loop that looks for the value of the array's address,
> deceides it is lower case, and then subtracts a fixed value from the character,
> if it is true(32?), since the difference between the little 'a' and the 'A'
> etc.... are always the same.

> Am I on the right path or wasting time with faulty logic?

> I am not asking for you to do my homework or send code, but rather if I am on
> the right track.

> Thanks, Greg

> --


--



Sun, 30 Jun 2002 03:00:00 GMT  
 A C function to capatalize first and last names.

Quote:

> If there is a function in the C library that checks for capitalization in an
> array of names, I DON'T want to know!
> Rather, I would like to beat my head against a wall, do it myself, and
> re-invent the wheel.
> I am thinking about a loop that looks for the value of the array's address,
> deceides it is lower case, and then subtracts a fixed value from the character,
> if it is true(32?), since the difference between the little 'a' and the 'A'
> etc.... are always the same.
> Am I on the right path or wasting time with faulty logic?

The latter.

The difference between 'A' and 'a' is 32 on some computers, but not on all.
The difference between 'B' and 'b' might equal the difference between 'A' and
'a', but then again, it might not. And what about all those alphabets in which
capitalization is impossible? Ok, those are rare.

Basically, you need to rely on the build-in functions to determine upper-
or lower-case. The built-in functions are the only place in which
knowledge about the local alphabet is stored. Look at isupper, toupper,
etc.

But first, get your problem clearer. "a loop that looks vor the value of
the array's address" ? What is that supposed to mean?

Michiel Salters
--



Sun, 30 Jun 2002 03:00:00 GMT  
 A C function to capatalize first and last names.

Quote:


> >I am thinking about a loop that looks for the value of the array's address,
> >deceides it is lower case, and then subtracts a fixed value from the character,
> >if it is true(32?), since the difference between the little 'a' and the 'A'
> >etc.... are always the same.

> >Am I on the right path or wasting time with faulty logic?

> You're on the right path. The only thing is the difference between 'a' and 'A'
> will not be the same on all systems. Convert 'a', 'A', and your char variable
> all to int, do the math, and then convert back to char.

NO!  char is an integral type, so converting to int will not help at all.
The real problem is that 'A' - 'a' != 'B' - 'b' on some systems.

/peter
--



Mon, 01 Jul 2002 03:00:00 GMT  
 A C function to capatalize first and last names.

[snip]

Quote:
>Also remember there are some names like McDonald with multiple
>capitalization.  It is easy to get it to work for most cases but there
>will probably be names that screws your algorithm up.

     And let us not forget the two surnames "MacKie" and "Mackie"
which are pronounced differently though spelled with the same letters
(case insensitively, of course).

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
     I have preferences.
     You have biases.
     He/She has prejudices.
--



Mon, 01 Jul 2002 03:00:00 GMT  
 A C function to capatalize first and last names.
Keep in mind that, in some character sets, some lowercase letters may
not have uppercase equivalents, and vice versa.  For example, in
Latin-1, probably the most common 8-bit extension to ASCII, there are
two lowercase letters that have no uppercase equivalent: character
0xdf, "LATIN SMALL LETTER SHARP S" and character 0xff, "LATIN SMALL
LETTER Y WITH DIAERESIS".

The tolower() and toupper() functions return their arguments if there
is no lowercase or uppercase equivalent -- which is probably exactly
what you want.

--

San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Welcome to the last year of the 20th century.
--



Thu, 04 Jul 2002 03:00:00 GMT  
 A C function to capatalize first and last names.
Quote:


...
> > I am thinking about a loop that looks for the value of the array's
address,
> > deceides it is lower case, and then subtracts a fixed value from the
character,
> > if it is true(32?), since the difference between the little 'a' and
the 'A'
> > etc.... are always the same.

> This sounds fairly reasonable, except that it is not always true that
the
> difference between capitals and lowers is always the same (e.g. EBCDIC
> computers).  Instead you should look at the macros defined in ctype.h,
> specifically islower() and toupper().

Actually in EBCDIC the problem is that the uppercase and lowercase
alphabets each are noncontiguous, but the offset between them *is*
constant (although different from that for ASCII).

However I concur that <ctype.h> is a more portable, more general,
safer, *and* clearer solution.  As long as you make sure the values
you use are not implicitly or explicitly signed char.

- David.Thompson1 now at worldnet.att.net
--



Sat, 06 Jul 2002 03:00:00 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. Get First and Last name

2. C as First Language for CS

3. First / Last Day of Week

4. REQ: advice for 2nd to last assignment for my first C class

5. AddNew() Update() over writes either first or last record

6. Making last line of text first visible in edit box

7. Making last line of text first visible in edit box

8. list[first,last) with ifstream_iterator

9. Newbie: separate big .cs file into small .cs files

10. Sorting a last names that are contained within a structure

11. Call a function in a function with name of a function as parameter

12. Error: First-chance exception in (KERNEL32.DLL) 0x80010108 (no name)

 

 
Powered by phpBB® Forum Software