如何转换C++;字符串到大写 我需要把C++中的字符串转换成全大写字母。我已经搜索了一段时间,找到了一种方法: #include <iostream> #include <algorithm> #include <string> using namespace std; int main() { string input; cin >> input; transform(input.begin(), input.end(), input.begin(), toupper); cout << input; return 0; } #包括 #包括 (最后两篇帖子)
增强字符串算法:如何转换C++;字符串到大写 我需要把C++中的字符串转换成全大写字母。我已经搜索了一段时间,找到了一种方法: #include <iostream> #include <algorithm> #include <string> using namespace std; int main() { string input; cin >> input; transform(input.begin(), input.end(), input.begin(), toupper); cout << input; return 0; } #包括 #包括 (最后两篇帖子),c++,string,uppercase,C++,String,Uppercase,增强字符串算法: #include <boost/algorithm/string.hpp> #include <string> std::string str = "Hello World"; boost::to_upper(str); std::string newstr = boost::to_upper_copy("Hello World"); #包括 #包括 std::string str=“Hello World”; 增压:至上止点(str); std
#include <boost/algorithm/string.hpp>
#include <string>
std::string str = "Hello World";
boost::to_upper(str);
std::string newstr = boost::to_upper_copy("Hello World");
#包括
#包括
std::string str=“Hello World”;
增压:至上止点(str);
std::string newstr=boost::to_upper_copy(“Hello World”);
试试这个小程序,直接从
#包括
#include您需要在toupper
前面加一个双冒号:
transform(input.begin(), input.end(), input.begin(), ::toupper);
说明:
有两种不同的toupper
功能:
全局命名空间中的toupper
(使用::toupper
访问),它来自C
std
命名空间中的toupper
(通过std::toupper
访问),该命名空间具有多个重载,因此不能仅用名称引用。您必须显式地将其强制转换为特定的函数签名才能被引用,但是获取函数指针的代码看起来很难看:static\u cast(&std::toupper)
由于您正在使用命名空间std,因此在编写toupper
时,2。隐藏1。因此,根据名称解析规则选择。您可以执行以下操作:
string name = "john doe"; //or just get string from user...
for(int i = 0; i < name.size(); i++) {
name.at(i) = toupper(name.at(i));
}
string name=“john doe”//或者只是从用户那里获取字符串。。。
对于(int i=0;i
您也可以使用下面代码中的函数将其转换为大写。
#include<iostream>
#include<cstring>
using namespace std;
//Function for Converting Lower-Case to Upper-Case
void fnConvertUpper(char str[], char* des)
{
int i;
char c[1 + 1];
memset(des, 0, sizeof(des)); //memset the variable before using it.
for (i = 0; i <= strlen(str); i++) {
memset(c, 0, sizeof(c));
if (str[i] >= 97 && str[i] <= 122) {
c[0] = str[i] - 32; // here we are storing the converted value into 'c' variable, hence we are memseting it inside the for loop, so before storing a new value we are clearing the old value in 'c'.
} else {
c[0] = str[i];
}
strncat(des, &c[0], 1);
}
}
int main()
{
char str[20]; //Source Variable
char des[20]; //Destination Variable
//memset the variables before using it so as to clear any values which it contains,it can also be a junk value.
memset(str, 0, sizeof(str));
memset(des, 0, sizeof(des));
cout << "Enter the String (Enter First Name) : ";
cin >> str; //getting the value from the user and storing it into Source variable.
fnConvertUpper(str, des); //Now passing the source variable(which has Lower-Case value) along with destination variable, once the function is successfully executed the destination variable will contain the value in Upper-Case
cout << "\nThe String in Uppercase = " << des << "\n"; //now print the destination variable to check the Converted Value.
}
#包括
#包括
使用名称空间std;
//用于将小写转换为大写的函数
void(字符str[],字符*des)
{
int i;
字符c[1+1];
memset(des,0,sizeof(des));//在使用变量之前先设置它。
for(i=0;i=97&&str[i]str;//从用户处获取值并将其存储到源变量中。
fnConvertUpper(str,des);//现在将源变量(具有小写值)与目标变量一起传递,一旦函数成功执行,目标变量将包含大写值
不能包含
使用名称空间std;
//用于将字符串转换为上限的函数
串穿器(串穿器){
对于(int i=0;i 使用位运算符将大写字母转换为小写字母,反之亦然
一,
string s=“cAPsLock”;
用于(字符和c:s)
c=c |“”;//类似于:c=tolower(c);
cout@LokiAstari:关键是你想要的是全局toupper
,而不是std::
一个,这突出了use namespace std
的问题——它用许多符号污染了默认名称空间,您不想只得到一个或两个符号。如果您只想导入一些符号,您应该只导入这些符号,而不是整个std
名称空间。@LokiAstari:不——如果您删除use namespace std;
然后您可以在该代码中使用toupper
,它将起作用(不需要:
)。当然,您需要使用std::transform添加;
(或者在转换之前粘贴std:
)另外,您可能需要#include
而不是#include
来获取全局命名空间中的toupper
,但这与其他所有内容无关。(
允许将toupper
放在全局命名空间中,但不需要)@Leems如果我只想将字符串中的第一个字符转换为大写,我该怎么办?我尝试了::toupper(输入[0]),但它不起作用。@HasanBasritoupper
返回修改后的字符,而不是就地更新。这意味着您需要重新分配结果:input[0]=toupper(输入[0])
。(这里,我没有把两个冒号放进去,这实际上意味着我想要<代码> STD::ToupP<代码> >使用名称空间STD< /COD>)。问题明确地要求C++的ScReund,您可能想考虑删除这个提交。STD::pTrIf Fun.()在C++ 11中被弃用,在C++ 14中被删除。
#include<iostream>
#include<cstring>
using namespace std;
//Function for Converting Lower-Case to Upper-Case
void fnConvertUpper(char str[], char* des)
{
int i;
char c[1 + 1];
memset(des, 0, sizeof(des)); //memset the variable before using it.
for (i = 0; i <= strlen(str); i++) {
memset(c, 0, sizeof(c));
if (str[i] >= 97 && str[i] <= 122) {
c[0] = str[i] - 32; // here we are storing the converted value into 'c' variable, hence we are memseting it inside the for loop, so before storing a new value we are clearing the old value in 'c'.
} else {
c[0] = str[i];
}
strncat(des, &c[0], 1);
}
}
int main()
{
char str[20]; //Source Variable
char des[20]; //Destination Variable
//memset the variables before using it so as to clear any values which it contains,it can also be a junk value.
memset(str, 0, sizeof(str));
memset(des, 0, sizeof(des));
cout << "Enter the String (Enter First Name) : ";
cin >> str; //getting the value from the user and storing it into Source variable.
fnConvertUpper(str, des); //Now passing the source variable(which has Lower-Case value) along with destination variable, once the function is successfully executed the destination variable will contain the value in Upper-Case
cout << "\nThe String in Uppercase = " << des << "\n"; //now print the destination variable to check the Converted Value.
}
#include <iostream>
using namespace std;
//function for converting string to upper
string stringToUpper(string oString){
for(int i = 0; i < oString.length(); i++){
oString[i] = toupper(oString[i]);
}
return oString;
}
int main()
{
//use the function to convert string. No additional variables needed.
cout << stringToUpper("Hello world!") << endl;
return 0;
}
string s = "cAPsLock";
for(char &c: s)
c = c | ' '; // similar to: c = tolower(c);
cout << s << endl; // output: capslock
string s = "cAPsLock";
for(char &c: s)
c = c & ~' '; // similar to: c = toupper(c);
cout << s << endl; // output: CAPSLOCK