2011-03-22 wcdj
Problem description:
I wanna to make a prog which convert small case letters entered by user to upper case letters .... I tried to use toupper function... but it only do action with a single character... is there any function which convert all characters like "lovely Hakeem" to "lovely Hakeem"
(Not by loop)
There is no way to convert all characters to upper or lower case without looping through the string.
Below are several common methods to convert uppercase letters to lowercase letters.
Method 1: character-by-character judgment, with uppercase letters + 32
# Include <cstdio> <br/> # include <cstring> <br/> int main () <br/>{< br/> char STR [128]; <br/> gets (STR); <br/> for (INT I = 0; I <strlen (STR); I ++) <br/> If (STR [I]> = 'A' & STR [I] <= 'Z') <br/> STR [I] + = 32; <br/> printf ("% s/n", STR); <br/> return 0; <br/>}< br/>
Method 2: Use the standard library algorithm Transform
# Include <string> <br/> # include <algorithm> <br/> # include <iostream> <br/> using namespace STD; <br/> int main () <br/>{< br/> string STR; <br/> CIN> STR; <br/> transform (Str. begin (), str. end (), str. begin (), tolower); <br/> cout <STR; <br/> return 0; <br/>}< br/>
Method 3: Use the function in ctype
/* Toupper. c: This program uses toupper and tolower to <br/> * analyze all characters between 0x0 and 0x7f. it also <br/> * applies _ toupper and _ tolower to any code in this <br/> * range for which these functions make sense. <br/> */<br/> # include <conio. h> <br/> # include <ctype. h> <br/> # include <string. h> <br/> char MSG [] = "some of these letters are capitals/R/N"; <br/> char * P; <br/> void main (void) <br/>{< br/> _ cputs (MSG); <br/>/* reverse case of message. */<br/> for (P = MSG; P <MSG + strlen (MSG); P ++) <br/>{< br/> If (islower (* p) <br/> _ putch (_ toupper (* p )); <br/> else if (isupper (* p) <br/> _ putch (_ tolower (* p )); <br/> else <br/> _ putch (* P ); <br/>}</P> <p> output <br/> some of these letters are capitals <br />
Method 4: Use the function (not ANSI standard) in string)
# Include <string. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char string [100] = "the string to end all strings! "; <Br/> printf (" mixed: % s/n ", string); <br/> _ strlwr (string ); // write the C library function into a smaller value <br/> printf ("lower: % s/n", string); <br/> _ strupr (string ); // The C-database function is capitalized. <br/> printf ("upper: % s/n", string); <br/>}< br/>
PS: a foreigner's Method
// You can do your own easily enough: <br/> // C <br/> # include <ctype. h> <br/> char * stoupper (char * s) <br/> {<br/> char * P = s; <br/> while (* P = toupper (* p) P ++; <br/> return S; <br/>}< br/> // C ++ <br/> # include <algorithm> <br/> # include <cctype> <br/> # include <functional> <br/> STD:: string & stoupper (const STD: string & S) <br/>{< br/> STD: String result (s); <br/> STD :: transform (<br/> S. begin (), <br/> S. end (), <br/> result. begin (), <br/> STD: ptr_fun <int, int> (STD: toupper) <br/>); <br/> return result; <br/>}< br/>/etc. hope this helps. <br/>
For more information, see:
Http://www.cplusplus.com/forum/general/33439/
Http://www.cplusplus.com/forum/beginner/14081/
Http://topic.csdn.net/u/20071015/21/4c266542-c3a6-4a5a-9754-fc7574ca5ef6.html