Board index » cppbuilder » Convert the First Character of a String to Upper Case?

Convert the First Character of a String to Upper Case?


2003-09-20 09:46:43 PM
cppbuilder30
Does anybody knows how to convert the first character of a string to upper
case? i have try the below function bet it does work for some reason.
please help
#define UPCASE_VALUE 27
//--------------------------------------------------------------------------
-
char *GetUpperCasedName(const char *pszStr)
{
char *pszRet;
int nLetter;
pszRet = strdup(pszStr);
nLetter = atoi(pszStr);
*pszRet=nLetter;
return pszRet;
}
James
 
 

Re:Convert the First Character of a String to Upper Case?

James wrote:
Quote
Does anybody knows how to convert the first character of a string to upper
case?
#include <ctype.h>
Quote
char *GetUpperCasedName(const char *pszStr)
{
pszStr [0] = toupper ( pszStr [0] );
Quote
return pszRet;

}
Hans.
 

Re:Convert the First Character of a String to Upper Case?

James wrote:
Quote
Does anybody knows how to convert the first character of a string to upper
case? i have try the below function bet it does work for some reason.
Please tell what it does instead.
#include <ctype.h>
Quote
char *GetUpperCasedName(const char *pszStr)
{
char *pszRet;
pszRet = strdup(pszStr);
pszRet [0] = toupper ( pszStr [0] );
Quote
return pszRet;

}
You have to free() your UpperCasedName after use.
Hans.
Quote


 

{smallsort}

Re:Convert the First Character of a String to Upper Case?

"James" < XXXX@XXXXX.COM >writes:
Quote
Does anybody knows how to convert the first character of a string to upper
case?
This problem sounds very easy, but in fact I think it's too difficult to be
solved in general.
To solve this problem correctly, you'll need
- understanding of the context
- a dictionary
- ...
The result will in general not contain the same number of characters as
the source text.
The ISO Standard C and C++ libraries offer some functionality that may do
what you are looking for if you are in a very specific context. There is
- the (1 argument) function std::toupper in <cctype>
- the (1 argument) function std::toupper in <locale>(for a single character)
- the (2 argument) function std::toupper in <locale>(for a string)
- the toupper member functions of the locale facet class template std::ctype
Note that for using the first of these functions, you have to make sure that
the argument has an unsigned char value, or your program will have undefined
behavior.
Quote
i have try the below function bet it does work for some reason.
Does it work for some reason? Or doesn't it? In the latter case, what does
"doesn't work" mean?
Quote
#define UPCASE_VALUE 27
What do you need this for?
Quote
//--------------------------------------------------------------------------
-
char *GetUpperCasedName(const char *pszStr)
{
char *pszRet;
int nLetter;

pszRet = strdup(pszStr);

nLetter = atoi(pszStr);
The Standard function atoi() takes a character string and attempts to convert
it into an int value, which it returns.
It doesn't treat just the first character; it treats the whole string. Expect
std::atoi("60") to return 60. This isn't what you are looking for.
 

Re:Convert the First Character of a String to Upper Case?

Hans Galema < XXXX@XXXXX.COM >writes:
Quote
#include <ctype.h>

>char *GetUpperCasedName(const char *pszStr)
>{
>char *pszRet;

>pszRet = strdup(pszStr);

pszRet [0] = toupper ( pszStr [0] );
This has undefined behavior if pszStr[0] isn't an unsigned char value.
 

Re:Convert the First Character of a String to Upper Case?

Thomas Maeder [TeamB] wrote:
Quote
>>char *GetUpperCasedName(const char *pszStr)
>>{
>>char *pszRet;
>
>>pszRet = strdup(pszStr);
>
>pszRet [0] = toupper ( pszStr [0] );


This has undefined behavior if pszStr[0] isn't an unsigned char value.
Do you mean with 'This' the value of toupper() or the assignment to
pszRet[0] ?
What do you consider an unsigned char value ? -128 to 127 ?
pszStr[0] can only have values -128 to 127 (assuming char is signed).
And toupper() will affect only 66 to 90. (a-z).
So no problems in practice.
<help>
toupper is a function that converts an integer ch (in the range EOF to 255)
to its uppercase value (A to Z; if it was lowercase, a to z). All others
are left unchanged.
</help>
Whatever the value of EOF is, I see only a-z changed.
Hans.
 

Re:Convert the First Character of a String to Upper Case?

Try this:
-----------------
#include <cstring>
#include <cctype>
using std::toupper;
using std::isalpha;
using std::strdup;
char *GetUpperCasedName(const char *pszStr)
{
char *pszRet;
if (!pszStr) // if calling arg is NULL, return NULL
return NULL;
else
{
pszRet = strdup(pszStr);
if (pszRet) // if strdup's allocation succeeded
{
unsigned char c = *pszRet;
if (isalpha(c)) // if 1st char is a letter
*pszRet = toupper(c);
}
}
return pszRet;
}
-----------------
. Ed
Quote
James wrote in message
news: XXXX@XXXXX.COM ...

Does anybody knows how to convert the first character of
a string to upper case?
 

Re:Convert the First Character of a String to Upper Case?

Hans Galema < XXXX@XXXXX.COM >writes:
Quote
Do you mean with 'This' the value of toupper() or the assignment to
pszRet[0] ?
I mean the implementation of GetUpperCasedName() you posted.
Quote
What do you consider an unsigned char value ? -128 to 127 ?
All values of x with 0<=x and x<=UCHAR_MAX
Quote
pszStr[0] can only have values -128 to 127 (assuming char is signed).
And toupper() will affect only 66 to 90. (a-z).
So no problems in practice.
If toupper() affected only the lower case characters (whose ASCII codes are
97 to 122, BTW), your solution would be simply wrong. Think of characters
such as ?and ?(I don't think that ?can be the first letter of a word in
German as written in Germany and Austria - in Switzerland, we don't use that
character).
But toupper() is declared
int toupper(int c);
Its requirements as per the ISO C and C++ Standards state that c has to
either have an unsigned char value or be equal to EOF, or the program has
undefined behavior.
 

Re:Convert the First Character of a String to Upper Case?

Thomas Maeder [TeamB] wrote:
Quote
>What do you consider an unsigned char value ? -128 to 127 ?

All values of x with 0<=x and x<=UCHAR_MAX
For better readibility: 0 <= x <= 255
Quote
>pszStr[0] can only have values -128 to 127 (assuming char is signed).
>And toupper() will affect only 66 to 90. (a-z).
>So no problems in practice.

If toupper() affected only the lower case characters (whose ASCII codes are
97 to 122, BTW),
For better readibility: 66 to 99 is A-Z. That was a mistake yes.
Quote
your solution would be simply wrong. Think of characters
such as ?and ?
For better readibility of this post you could have provided their
ascii codes too. Are they in the range 127 < x <= 255 ? They cannot
occur in the const char *pszStr.
Quote
But toupper() is declared

int toupper(int c);

Its requirements as per the ISO C and C++ Standards state that c has to
either have an unsigned char value or be equal to EOF, or the program has
undefined behavior.
What I was stating is that if GetUpperCasedName() if declared as
GetUpperCasedName(const char *pszStr)
that the first letter can only have values of -127 to 127.
If you apply toupper() to that range, in practice only 97 to 122
are affected. So I see no undefined behaviour. To show undefined
behaviour is no obligation for the compiler manufacturer.
If James wanted the whole asciirange allowed for his names he should
have declared GetUpperCasedName() as
GetUpperCasedName(const unsigned char *pszStr);
Hans.
 

Re:Convert the First Character of a String to Upper Case?

Hans Galema < XXXX@XXXXX.COM >writes:
Quote
Thomas Maeder [TeamB] wrote:

>>What do you consider an unsigned char value ? -128 to 127 ?
>All values of x with 0<=x and x<=UCHAR_MAX

For better readibility: 0 <= x <= 255
Actually, this diminishes readibility.
And it's wrong on C++ implementations where UCHAR_MAX!=255. I wrote UCHAR_MAX
because I meant UCHAR_MAX.
Quote
>>pszStr[0] can only have values -128 to 127 (assuming char is signed).
>>And toupper() will affect only 66 to 90. (a-z).
>>So no problems in practice.

>
>If toupper() affected only the lower case characters (whose ASCII codes are
>97 to 122, BTW),

For better readibility: 66 to 99 is A-Z. That was a mistake yes.
Actually, 'A' has code ASCII 65, not 66.
Quote
>your solution would be simply wrong. Think of characters
>such as ?and ?

For better readibility of this post you could have provided their
ascii codes too.
?and ?don't have ASCII codes.
Quote
Are they in the range 127 < x <= 255?
In some often used character encodings (such as ISO-8895-1 (a.k.a. Latin-1)
and ISO-8895-15), they are encoded by values in that range yes.
Quote
They cannot occur in the const char *pszStr.
In C, the entire range of char is often used for encoding characters. The
negative char values are usually used to encode characters whose encodings
lie between CHAR_MAX and UCHAR_MAX; the corresponding code in the encoding
used can be computed by adding UCHAR_MAX to the char value.
Quote
What I was stating is that if GetUpperCasedName() if declared as
GetUpperCasedName(const char *pszStr)
that the first letter can only have values of -127 to 127.
If you apply toupper() to that range, in practice only 97 to 122
are affected.
No. If you apply toupper() to that range, you get undefined behavior for
all negative values, since these are not unsigned char values.
Quote
If James wanted the whole asciirange allowed for his names he should
have declared GetUpperCasedName() as
GetUpperCasedName(const unsigned char *pszStr);
Not necessarily.
 

Re:Convert the First Character of a String to Upper Case?

Thomas Maeder [TeamB] wrote:
Quote
>that the first letter can only have values of -127 to 127.
>If you apply toupper() to that range, in practice only 97 to 122
>are affected.

No. If you apply toupper() to that range, you get undefined behavior for
all negative values, since these are not unsigned char values.
Only one of these statements can be true. In practice I see that all
negative values are unaffected.
Hans.
 

Re:Convert the First Character of a String to Upper Case?

Hans Galema < XXXX@XXXXX.COM >writes:
Quote
Only one of these statements can be true.
Which statements?
Quote
In practice I see that all negative values are unaffected.
I am not going against your accepting undefined behavior in your programs.
I just like it to be highlighted in posts to newsgroups such as this one.
 

Re:Convert the First Character of a String to Upper Case?

Thomas Maeder [TeamB] wrote:
Quote
Which statements?
About .. apply toupper()..
Quote
>In practice I see that all negative values are unaffected.

I am not going against your accepting undefined behavior in your programs.
I do not want undefined behavior either, and of course you should not read
my post in such a way. I had seen the help about toupper() too. Made some
testroutines and saw that it never affected negative values. (So I came with
the remark on the compiler manufacturer). Now I know that such test is no
proof that it never will happen. But it was enough for me to state that in
practice it did not matter.
Now it would be interesting if you had other experiences or could
supply an example where things ran wrong.
Hans.
 

Re:Convert the First Character of a String to Upper Case?

Hans Galema < XXXX@XXXXX.COM >writes:
Quote
Now it would be interesting if you had other experiences or could
supply an example where things ran wrong.
I haven't made such experience.
I tend to read documentation before using a function. The <cctype>functions
of the C and C++ Standard Libraries clearly document what the requirements
of their arguments are, so I take care to comply to these requirements.
 

Re:Convert the First Character of a String to Upper Case?

"Thomas Maeder [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
I am not going against your accepting undefined behavior
in your programs.
It is not undefined behavior. Why do you claim that it is? toupper() only
looks at a specific range of values, a range that is valid for both signed
and unsigned data types. Anything outside of that range is ignored,
returned as-is without modification. That is not undefined behavior. That
is very defined behavior.
Gambit