简体   繁体   English

c中的小写字符串

[英]lower case string in c

i had a few line here : 我在这里有几行:

#include <stdio.h>

char *tolower(char *data)
{
    char *p = data;
    while(*p)
    {
        printf("nilai p : %c\n",*p);
            if(*p >= 'A' && *p <= 'Z')
            {
                *p += ('a' - 'A');
            }
        p++;
    }
    return p;

}

int main()
{

    char *a = "HajAR BleH";
    char *b = tolower(a);

    printf("nilai b : %s\n",b);
    printf("nilai a - A : %d\n",'a' - 'A');
return 0;
}

next, compiled, running on gdb, and segmentation traced 接下来,进行编译,在gdb上运行,并跟踪分段

xxx@aaa:/tmp$ gcc -o aa aa.c --debug
xxx@aaa:/tmp$ gdb -q aa
Reading symbols from /tmp/aa...done.
(gdb) r
Starting program: /tmp/aa 
nilai p : H

Program received signal SIGSEGV, Segmentation fault.
0x0804841e in tolower (data=0x804855e "HajAR BleH") at aa.c:11
11                  *p += ('a' - 'A');
(gdb) 

question

1. i think *p += ('a' - 'A'); 1.我认为*p += ('a' - 'A'); will equal as 'H' += ('a' - 'A') and equal with 72 += 32 but, accidentally segmentation fault, how come it could be ? 将等于'H' += ('a' - 'A')等于72 += 32但是,偶然的分割错误,怎么可能呢?

2. why need to add ('a' - 'A') to make char / byte get lower ? 2.为什么需要添加('a'-'A')来使char / byte变小?

thats all for now, thank in advance 多数民众赞成在现在,在此先感谢

You are trying to modify a string literal. 您正在尝试修改字符串文字。 Change: 更改:

char *a = "HajAR BleH";

to: 至:

char a[] = "HajAR BleH";

Also, there already is a standard library function called tolower(), which you should in fact be using in your code. 另外,已经有一个名为tolower()的标准库函数,实际上您应该在代码中使用它。 Call your own function something else. 调用其他函数。

The most likely cause of the SEGV is that the compiler has placed a in read-only memory, as it is allowed to do with string literals. 在最有可能的原因SEGV是编译器已经放在a只读存储器,因为它允许用字符串文字做。

Try changing a 's definition to: 尝试改变a的定义:

char a[] = "HajAR BleH";

As a matter of style, there already exists a standard function called tolower , which does something different (convert one character to lowercase). 就样式而言,已经存在一个称为tolower的标准函数,该函数的功能有所不同(将一个字符转换为小写字母)。 With this in mind, I'd suggest: 考虑到这一点,我建议:

  1. giving your function a different name to avoid confusion; 为您的函数起一个不同的名字,以避免混淆;
  2. using tolower in your implementation to convert one character to lowercase. 在实现中使用tolower将一个字符转换为小写。

The reason for the seg-fault is you are trying to modify a string literal which is resides in read only memory leading to undefined behavior. seg-fault的原因是您试图修改驻留在只读存储器中的字符串文字,从而导致未定义的行为。

Try changing 尝试改变

char *a = "HajAR BleH";

to

char a[] = "HajAR BleH";

As well as the problem with the read-only memory, you need to return data rather than p . 以及只读存储器的问题,您需要返回data而不是p By the time your loop has finished, p points to the null-terminator. 在循环结束时, p指向空终止符。

1: Essentially your code is correct, however you're trying to modify a constant string ( char *a should reda const char *a ). 1:从本质上来说,您的代码是正确的,但是您尝试修改常量字符串( char *a应该reda const char *a )。 You'll have to create another character array you'll be able to modify. 您必须创建另一个可以修改的字符数组。

2: ('a' - 'A') calculates the "difference" in the ascii values between lower case characters and upper case characters (32). 2 :( ('a' - 'A')计算小写字符和大写字符之间的ascii值中的“差异”(32)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM