C/C++ library function (tolower/toupper) implements case conversion of letters
This article will introduce the library function to implement the case conversion of letters, commonly used in the type. H (c ++ is cctype) library file under the definition of function methods. First let’s look at the prototype implementation of the tolower/toupper function under C:
int tolower(int c)
{
if ((c >= 'A') && (c <= 'Z'))
return c + ('a' - 'A');
return c;
}
int toupper(int c)
{
if ((c >= 'a') && (c <= 'z'))
return c + ('A' - 'a');
return c;
}
And I’m going to do that with two little demos.
C implementation:
#include<string.h> //strlen
#include<stdio.h> //printf
#include<ctype.h> //tolower
int main()
{
int i;
char string[] = "THIS IS A STRING";
printf("%s\n", string);
for (i = 0; i < strlen(string); i++)
{
string[i] = tolower(string[i]);
}
printf("%s\n", string);
printf("\n");
}
Save as xxX. c file, execute: GCC-O XXX xxX. c generate execute file XXX. Operation:./ XXX
above is the implementation of C. Similarly, the implementation under C++ is as follows:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string str= "THIS IS A STRING";
for (int i=0; i <str.size(); i++)
str[i] = tolower(str[i]);
cout<<str<<endl;
return 0;
}
Save as xxx. CPP, execute g++ xxx. CPP to generate the execution file a.ut, execute a.ut, and the effect is as follows:
The demo above
implements the upper case tolower case conversion, again, the lower case toupper case conversion is the same, just replace tolower with toupper.
Read More:
- C / C + + rounding function ceil(), floor ()
- C / C + + rounding function: round function
- C++ error: jump to case label crosses initialization
- Differences between length() size() and C strlen() of C + + string member functions
- VS2010 library function problem: objidl. H (11266): error c2061: syntax error: identifier ‘__ RPC__ out_ xcount_ part’
- C++ [Error] jump to case label [-fpermissive]
- C language string processing error warning, c4996, sprintf, predicted, c4996, strcpy, c4996, strcat
- C++:error C2228: left of ‘.str’ must have class/struct/union
- The use of C + + template function and lambda expression
- How to use C + + function pointer array
- [C + +] C + + overload operator = must be a nonstatic member function?
- The function and usage of argc and argv in C language
- Error c2137 of C language: empty character constant (Fixed)
- error C2057: expected constant expression (Can the size of an array in C language be defined when the program is running?)
- Python calls C to generate so library and reports an error: undefined symbol
- C error C2143 syntax error missing before ‘type’
- Solve the problem of error: cannot pass objects of non trivially copyable type ‘STD:: String’ in C / C + +
- The function of structured shuffleplit() in SK learn realizes the division of data set
- C language write() function analysis: write failed bad address
- error C4996: ‘scanf‘: This function or variable may be unsafe.Visual Studio Series compilers report errors using scanf function