Board index » cppbuilder » Convert the First Character of a String to Upper Case?
James
![]() CBuilder Developer |
James
![]() CBuilder Developer |
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 |
Hans Galema
![]() CBuilder Developer |
2003-09-20 09:56:14 PM
Re:Convert the First Character of a String to Upper Case?
James wrote:
QuoteDoes anybody knows how to convert the first character of a string to upper Quotechar *GetUpperCasedName(const char *pszStr) Quotereturn pszRet; |
Hans Galema
![]() CBuilder Developer |
2003-09-20 10:03:10 PM
Re:Convert the First Character of a String to Upper Case?
James wrote:
QuoteDoes anybody knows how to convert the first character of a string to upper Quotechar *GetUpperCasedName(const char *pszStr) Quotereturn pszRet; Quote{smallsort} |
maeder
![]() CBuilder Developer |
2003-09-20 10:12:59 PM
Re:Convert the First Character of a String to Upper Case?
"James" < XXXX@XXXXX.COM >writes:
QuoteDoes anybody knows how to convert the first character of a string to upper 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. Quotei have try the below function bet it does work for some reason. Quote#define UPCASE_VALUE 27 Quote//-------------------------------------------------------------------------- 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. |
maeder
![]() CBuilder Developer |
2003-09-20 11:24:06 PM
Re:Convert the First Character of a String to Upper Case?
Hans Galema < XXXX@XXXXX.COM >writes:
Quote#include <ctype.h> |
Hans Galema
![]() CBuilder Developer |
2003-09-21 12:42:16 AM
Re:Convert the First Character of a String to Upper Case?
Thomas Maeder [TeamB] wrote:
Quote>>char *GetUpperCasedName(const char *pszStr) 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. |
Ed Mulroy [TeamB]
![]() CBuilder Developer |
2003-09-21 03:16:02 AM
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 QuoteJames wrote in message |
maeder
![]() CBuilder Developer |
2003-09-21 03:16:30 PM
Re:Convert the First Character of a String to Upper Case?
Hans Galema < XXXX@XXXXX.COM >writes:
QuoteDo you mean with 'This' the value of toupper() or the assignment to QuoteWhat do you consider an unsigned char value ? -128 to 127 ? QuotepszStr[0] can only have values -128 to 127 (assuming char is signed). 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. |
Hans Galema
![]() CBuilder Developer |
2003-10-05 11:01:35 PM
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 ? Quote>pszStr[0] can only have values -128 to 127 (assuming char is signed). Quoteyour solution would be simply wrong. Think of characters occur in the const char *pszStr. QuoteBut toupper() is declared 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. |
maeder
![]() CBuilder Developer |
2003-10-05 11:33:42 PM
Re:Convert the First Character of a String to Upper Case?
Hans Galema < XXXX@XXXXX.COM >writes:
QuoteThomas Maeder [TeamB] wrote: because I meant UCHAR_MAX. Quote>>pszStr[0] can only have values -128 to 127 (assuming char is signed). Quote>your solution would be simply wrong. Think of characters QuoteAre they in the range 127 < x <= 255? QuoteThey cannot occur in the const char *pszStr. 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. QuoteWhat I was stating is that if GetUpperCasedName() if declared as QuoteIf James wanted the whole asciirange allowed for his names he should |
Hans Galema
![]() CBuilder Developer |
2003-10-06 02:02:38 AM
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. Hans. |
maeder
![]() CBuilder Developer |
2003-10-06 02:15:50 AM
Re:Convert the First Character of a String to Upper Case?
Hans Galema < XXXX@XXXXX.COM >writes:
QuoteOnly one of these statements can be true. QuoteIn practice I see that all negative values are unaffected. |
Hans Galema
![]() CBuilder Developer |
2003-10-06 03:55:30 AM
Re:Convert the First Character of a String to Upper Case?
Thomas Maeder [TeamB] wrote:
QuoteWhich statements? Quote>In practice I see that all negative values are unaffected. 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. |
maeder
![]() CBuilder Developer |
2003-10-06 04:07:22 AM
Re:Convert the First Character of a String to Upper Case?
Hans Galema < XXXX@XXXXX.COM >writes:
QuoteNow it would be interesting if you had other experiences or could 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. |
Remy Lebeau (TeamB)
![]() CBuilder Developer |
2003-10-06 05:25:41 AM
Re:Convert the First Character of a String to Upper Case?
"Thomas Maeder [TeamB]" < XXXX@XXXXX.COM >wrote in message
QuoteI am not going against your accepting undefined behavior 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 |