Board index » cppbuilder » Beginning Word UpperCase ( ) Buggy code..ignores german characters..

Beginning Word UpperCase ( ) Buggy code..ignores german characters..


2003-07-10 06:52:52 AM
cppbuilder95
Hi Remy, first thanks alot for the code you posted to me..it works...
but it ignores important german letters (The programm i'm develop is a
german one)
the code ignores: "?, "?, "? and "?
(I hope you can see those charecters in your mail programm..?)
do you have any ideas how to solve it...?
here's your code..
/*********************************************************/
#include <ctype.h>
#define IS_WHITESPACE(c) ( iswspace(c) || (c == ',') || (c == '.') )
void __fastcall TForm1::MakeThemUppercaseClick(TObject *Sender)
{
Memo1->Lines->BeginUpdate();
try
{
AnsiString MyText = Memo1->Text.LowerCase();
char *ptr = MyText.c_str();
if( ptr )
{
// scan through to the end of the text
while( *ptr != '\0' )
{
// skip over any white space characters
while( (*ptr != '\0') && IS_WHITESPACE(*ptr) ) ++ptr;
if( *ptr != '\0' )
{
// uppercase the first non-whitespace character
*ptr = toupper(*ptr);
// skip over to the next white space character
while( (*ptr != '\0') && !IS_WHITESPACE(*ptr) )
++ptr;
}
}
Memo1->Text = MyText;
}
}
__finally {
Memo1->Lines->EndUpdate();
}
}
 
 

Re:Beginning Word UpperCase ( ) Buggy code..ignores german characters..

Oren (Halvani.de) wrote:
Quote
the code ignores: "?, "?, "? and "?
So you have problems with toupper() not making them upper ?
As I understand you correct AnsiString::LowerCase() handles
the uppers of them ok ?
The firstproblem this code gives is that
Quote
#define IS_WHITESPACE(c) ( iswspace(c) || (c == ',') || (c == '.') )
wil consider "?, "?, and "? as whitespace, as you can find out
very easy.
The second problem is that toupper did not handle the remaining "?.
toupper() does not handle any of "?, "?, "? and "?.
Solution:
#define IS_WHITESPACE(c) ((c==' ')||(c=='\r')||(c=='\n')||(c== ',')||(c=='.'))
Quote
if( *ptr != '\0' )
{
// uppercase the first non-whitespace character
*ptr = toupper(*ptr);
change that to
*(unsigned char*)ptr = germantoupper ( *(unsigned char*)ptr );
with
unsigned char germantoupper ( unsigned char letter )
{
/* To see what happens:
// #include <stdio>
unsigned char buffer [200];
sprintf ( buffer, "(%c) %d %x", letter, (int)letter, (int)letter );
Form1->Memo3->Lines->Add ( (char*)buffer );
*/
//"?, "?, "? and "?
if ( letter == '? ) return 'O'; // does nothing
if ( letter == '? ) return 'A'; // does nothing
if ( letter == '? ) return 'U'; // does nothing
if ( letter == '? ) return 'S'; // does nothing
if ( letter == 246 ) return 'O'; // please add the umlaut
if ( letter == 228 ) return 'A'; // please add the umlaut
if ( letter == 252 ) return 'U'; // please add the umlaut
if ( letter == 223 ) return 'S'; // please change proper
return toupper ( letter );
}
I had this in Memo1;
char orenhalvanitext2 []=
"the code ignores: ? ? and \r\n"
"the code ignores: ooooo, aaaaaaa, uuuu and sssssssss\r\n";
With Memo2->Text = MyText;
Memo2:
The Code Ignores: O, A, U?And S
The Code Ignores: Ooooo, Aaaaaaa, Uuuu And Sssssssss
Memo3:
(t) 116 74
(c) 99 63
(i) 105 69
(? 246 f6
(? 228 e4
(? 252 fc
(a) 97 61
(? 223 df
(t) 116 74
(c) 99 63
(i) 105 69
(o) 111 6f
(a) 97 61
(u) 117 75
(a) 97 61
(s) 115 73
You don't need the lines
Memo1->Lines->BeginUpdate();
Memo1->Lines->EndUpdate();
Hans.
 

Re:Beginning Word UpperCase ( ) Buggy code..ignores german characters..

"Oren \(Halvani.de\)" < XXXX@XXXXX.COM >wrote:
Quote
Hi Remy, first thanks alot for the code you posted to me..it
works... but it ignores important german letters (The programm
i'm develop is a german one)

the code ignores: "?, "?, "? and "?
#define IS_WHITESPACE(c) ( iswspace(c) || (c == ',') || (c>*ptr = toupper(*ptr);
Use the locale versions of iswspace and toupper instead. These
are std::toupper(char, locale) and std::iswspace(char, locale).
Due to an error in BCB headers you will have to replace std::
be _STL:: however.
I have written a function to capitalize words in a string only
some days ago. Maybe you would like to use that:
#include <locale>
char* CapitalizeString(char* pSourceString)
{
char* pString = pSourceString;
bool LastCharWasDelimiter = true;
while(*pSourceString)
{
if(LastCharWasDelimiter)
*pSourceString = (char)_STL::toupper(*pSourceString, std::locale(""));
else
*pSourceString = (char)_STL::tolower(*pSourceString, std::locale(""));
LastCharWasDelimiter = !_STL::isalnum(*pSourceString, std::locale(""));
++pSourceString;
}
return pString;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Memo1->Text = CapitalizeString(Memo1->Text.c_str());
}
 

{smallsort}