string大小写转换函数

本文介绍如何在C++中使用STL库函数transform来实现字符串的大小写转换,并解决了在使用g++编译时遇到的编译错误问题。文章提供了两种解决方法:一种是通过类型转换将toupper宏转换为函数指针;另一种是自定义转换函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近被多线程+野指针折磨ING……

    C++中没有string直接转换大小写的函数,需要自己实现。一般来讲,可以用stl的algorithm实现:

#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;
}


    但在使用g++编译时会报错:
对 ‘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>)’ 的调用没有匹配的函数。
    这里出现错误的原因是Linux将toupper实现为一个宏而不是函数
/usr/lib/syslinux/com32/include/ctype.h:

/* Note: this is decimal, not hex, to avoid accidental promotion to unsigned */
#define _toupper(__c) ((__c) & ~32)
#define _tolower(__c) ((__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;
}


    两种解决方案:

1.transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper);

    这里(int (*)(int))toupper是将toupper转换为一个返回值为int,参数只有一个int的函数指针。

2.自己实现ToUpper函数:
int ToUpper(int c)
{
    return toupper(c);
}
transform(str.begin(), str.end(), str.begin(), ToUpper);

附:大小写转换函数
#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);
}

### C语言实现字符串首字母大小写转换 在C语言中,可以自定义函数来实现字符串首字母的大写或小写换。下面展示两种常见的方式。 #### 首字母大写 通过判断第一个字符是否为小写字母并将其换为大写: ```c #include <stdio.h> #include <ctype.h> void capitalize_first(char *str) { if (*str >= 'a' && *str <= 'z') { // 判断是否为小写字母[^1] *str = *str - ('a' - 'A'); // 换为首字母大写 } } int main() { char sentence[] = "hello world"; capitalize_first(sentence); printf("%s\n", sentence); // 输出 Hello world return 0; } ``` #### 首字母小写 同样地,也可以创建一个函数用来把字符串的第一个字符变为小写形式: ```c #include <stdio.h> #include <ctype.h> void lowercase_first(char *str) { if (*str >= 'A' && *str <= 'Z') { // 判断是否为大写字母 *str = *str + ('a' - 'A'); // 换为首字母小写 } } int main() { char sentence[] = "HELLO WORLD"; lowercase_first(sentence); printf("%s\n", sentence); // 输出 hELLO WORLD return 0; } ``` ### Python 实现字符串首字母大小写转换 对于Python而言,内置了多种方法可以直接处理这类需求。 - 使用`capitalize()`可使整个字符串仅保留开头一个大写字母其余全部变更为小写[^2] ```python text = "hello world" print(text.capitalize()) # 输出 Hello world ``` - 若要单独改变首个字符而不影响其他部分,则可以通过切片组合的方式来完成: ```python def change_case(s, to_upper=True): if not s: return "" first_char = s[0].upper() if to_upper else s[0].lower() rest_of_string = s[1:] return f"{first_char}{rest_of_string}" # 测试案例 original_text = "hello world" new_text = change_case(original_text, True) print(new_text) # 输出 Hello world ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值