C语言中如何将字符串小写?

发布于 2024-08-28 23:23:05 字数 32 浏览 4 评论 0原文

在 C 中如何将混合大小写字符串转换为小写字符串?

How can I convert a mixed case string to a lowercase string in C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

够运 2024-09-04 23:23:05

它位于标准库中,这是我所看到的实现此类功能的最直接的方法。所以是的,只需循环字符串并将每个字符转换为小写即可。

像这样的小事:

#include <ctype.h>

for(int i = 0; str[i]; i++){
  str[i] = tolower(str[i]);
}

或者如果您更喜欢一种衬垫,那么您可以使用 JF Sebastian 的这款:

for ( ; *p; ++p) *p = tolower(*p);

It's in the standard library, and that's the most straight forward way I can see to implement such a function. So yes, just loop through the string and convert each character to lowercase.

Something trivial like this:

#include <ctype.h>

for(int i = 0; str[i]; i++){
  str[i] = tolower(str[i]);
}

or if you prefer one liners, then you can use this one by J.F. Sebastian:

for ( ; *p; ++p) *p = tolower(*p);
错爱 2024-09-04 23:23:05

如果您将自己限制为 ASCII,则转换为小写相当于上升位 0x60:

for(char *p = pstr; *p; ++p)
    *p = *p > 0x40 && *p < 0x5b ? *p | 0x60 : *p;

to convert to lower case is equivalent to rise bit 0x60 if you restrict yourself to ASCII:

for(char *p = pstr; *p; ++p)
    *p = *p > 0x40 && *p < 0x5b ? *p | 0x60 : *p;
风吹雪碎 2024-09-04 23:23:05

循环指针以获得更好的性能:

#include <ctype.h>

char* toLower(char* s) {
  for(char *p=s; *p; p++) *p=tolower(*p);
  return s;
}
char* toUpper(char* s) {
  for(char *p=s; *p; p++) *p=toupper(*p);
  return s;
}

Looping the pointer to gain better performance:

#include <ctype.h>

char* toLower(char* s) {
  for(char *p=s; *p; p++) *p=tolower(*p);
  return s;
}
char* toUpper(char* s) {
  for(char *p=s; *p; p++) *p=toupper(*p);
  return s;
}
坐在坟头思考人生 2024-09-04 23:23:05

如果我们要像使用 tolower() 一样草率,请执行以下操作:

char blah[] = "blah blah Blah BLAH blAH\0";
int i = 0;
while( blah[i] |=' ', blah[++i] ) {}

但是,好吧,如果您向它提供一些符号/数字,它就会爆炸,并且通常它是邪恶的。不过,面试问题很好。

If we're going to be as sloppy as to use tolower(), do this:

char blah[] = "blah blah Blah BLAH blAH\0";
int i = 0;
while( blah[i] |=' ', blah[++i] ) {}

But, well, it kinda explodes if you feed it some symbols/numerals, and in general it's evil. Good interview question, though.

只为一人 2024-09-04 23:23:05

我是 C 新手,一直在努力将字符串从大写转换为小写。我错误地更改了字符串 'A' + 32 = 'a'。而且我无法解决这个问题。

我使用了 char 类型,最后我能够将其转换为 string 类型。您可以咨询:

#include <ctype.h>
#include <stdio.h>
#include <string.h>

string convert_lower(string str)
{
    int length = strlen(str);
    char c[length + 1];

    for (int i = 0; i < length; i++)
    {
        if (str[i] <= 'Z' || str[i] >= 'A')
        {
            c[i] = tolower((char)str[i]);
        }
    }
    c[length] = '\0';
    string text = c;
    return text;
}

I'm new to C and have been trying hard to convert strings from uppercase to lowercase. I made the mistake of changing the string 'A' + 32 = 'a'. And I can't solve the problem.

I used char type and finally I was able to convert it to string type. You can consult:

#include <ctype.h>
#include <stdio.h>
#include <string.h>

string convert_lower(string str)
{
    int length = strlen(str);
    char c[length + 1];

    for (int i = 0; i < length; i++)
    {
        if (str[i] <= 'Z' || str[i] >= 'A')
        {
            c[i] = tolower((char)str[i]);
        }
    }
    c[length] = '\0';
    string text = c;
    return text;
}
聽兲甴掵 2024-09-04 23:23:05

您可以使用这个用户定义的函数:

char *ToLower(char *text){

    int stringlength=strlen(text);
    char *lower=(char*)malloc(sizeof(char)*stringlength);
    int i=0;

    while(i<stringlength){
        //checking if the character is an uppercase
        if((text[i]-'A')<=25){

            lower[i]=text[i]+32;
        }
        //if the character is lowercase
        else{

            lower[i]=text[i];
        }

        i++;
    }

    return lower;
}

You may use this user defined function:

char *ToLower(char *text){

    int stringlength=strlen(text);
    char *lower=(char*)malloc(sizeof(char)*stringlength);
    int i=0;

    while(i<stringlength){
        //checking if the character is an uppercase
        if((text[i]-'A')<=25){

            lower[i]=text[i]+32;
        }
        //if the character is lowercase
        else{

            lower[i]=text[i];
        }

        i++;
    }

    return lower;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文