Advertisement

[std::string] Best case conversion

Started by March 13, 2001 10:13 PM
1 comment, last by gimp 23 years, 10 months ago
I have the feeling that my upper\lower code is dodgy so I''d like to know if there is a better way to do this: [source void Upper(std::string &a_Data) { for (unsigned long i=0; i > 96) && (a_Data < 123)) a_Data -= 32; } void Lower(std::string &a_Data) { for (unsigned long i=0; i<a_Data.size(); i++) if ((a_Data > 64) && (a_Data < 91)) a_Data += 32; } [/source] I was reading about recently and though that that might be cool. Is there a safer ''standard'' way of doing this? Many thanks Chris </i>
Chris Brodie
Well if you know you only have letters, you can just do this:
      for(int i=0;szStr[i]!=0;i++)	{	szStr[i] &= ~32;	}        


Otherwise you also have to check:
      for(int i=0;szStr[i]!=0;i++)	{	if(szStr[i]>96 && szStr[i]<123)		szStr[i] &= ~32;	}      


PS That does ToUpper
PSS I'm almost certain that std:string has an Upper & Lower function built-in
Magmai Kai Holmlor
- The disgruntled & disillusioned


Edited by - Magmai Kai Holmlor on March 13, 2001 11:51:52 PM

Edited by - Magmai Kai Holmlor on March 13, 2001 11:54:07 PM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
The correct way is to use toupper and tolower. Your code will break if somebody is using anything other than plain 7-bit ascii.

I pulled this out of an stl book I''ve got:
// lowercase all characterstransform(s.begin(), s.end(), s.begin(), tolower); 


I don''t think std::string as either uppercasing/lowercasing or case-insensitive comparisons built in. I needed them once and after spending quite a bit of time trying to find them I gave up and wrote my own. I could have missed them I suppose.

-Mike
-Mike

This topic is closed to new replies.

Advertisement