Can I convert uppercase and lowercase letters without repeating them?

Source: Internet
Author: User

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

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.