String case Conversion Function

Source: Internet
Author: User

String case-insensitive conversion functions-blog channel-csdn. net

String case Conversion Function Category: C/C ++/C # Read by 1495 Comment (0) Favorites Report Stringbasicalgorithmc multi-thread function

Recently suffering from multithreading and wild pointers ing ......

C ++ does not have a string function to convert the case sensitivity directly. You must implement this function by yourself. Generally, you can use the algorithm of STL to implement:

# Include <iostream>
# Include <cctype>
# Include <string>
# Include <algorithm>

Using namespace STD;

Int main ()
{
String S = "ddkfjsldjl ";
Transform (S. Begin (), S. End (), S. Begin (), toupper );
Cout <S <Endl;
Return 0;
}

However, when compiling with G ++, an error is returned:
For 'transform (_ gnu_cxx ::__ normal_iterator <char *, STD: basic_string <char, STD: char_traits <char>, STD: Allocator <char >>, _ gnu_cxx ::__ normal_iterator <char *, STD: basic_string <char, STD: char_traits <char>, STD: Allocator <char >>,_ gnu_cxx: :__ normal_iterator <char *, STD: basic_string <char, STD: char_traits <char>, STD: Allocator <char >>, <unresolved overloaded function type>).
The error occurs because Linux implements toupper as a macro instead of a function.:
/Usr/lib/syslinux/com32/include/ctype. h:

/* Note: This is decimal, not hex, to avoid accidental promotion to unsigned */
# DEFINE _ toupper (_ C )&~ 32)
# DEFINE _ tolower (_ C) | 32)

_ Ctype_inline int toupper (INT _ C)
{
Return islower (_ C )? _ Toupper (_ C): _ C;
}

_ Ctype_inline int tolower (INT _ C)
{
Return isupper (_ C )? _ Tolower (_ C): _ C;
}

Two solutions:

1.Transform (Str. Begin (), str. End (), str. Begin (), (INT (*) (INT) toupper );

Here (INT (*) (INT) toupper converts toupper into a function pointer with a return value of Int. The parameter has only one Int.

2. Implement the toupper function by yourself:
Int toupper (int c)
{
Return toupper (C );
}
Transform (Str. Begin (), str. End (), str. Begin (), toupper );

Appendix: case-sensitive Conversion Function
# Include <cctype>
# Include <string>
# Include <algorithm>

Using namespace STD;

Void toupperstring (string & Str)
{
Transform (Str. Begin (), str. End (), str. Begin (), (INT (*) (INT) toupper );
}

Void tolowerstring (string & Str)
{
Transform (Str. Begin (), str. End (), str. Begin (), (INT (*) (INT) tolower );
}

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.