c++使用 tolower 时 cstrings 出现格式错误

发布于 2024-11-02 19:28:10 字数 504 浏览 3 评论 0原文

您好,所以我尝试采用 cstring 并将其设置为小写,但是当我在最后打印 cstring 时,我收到一个奇怪的格式框,其中一些字母应该在其中。有人有什么想法吗?

#include <string>
#include <iostream>
#include <string.h>

using namespace std;

int main () 
{ 
    int i=0; 
    char* str="TEST"; 
    char c; 
    char* cstr = new char[strlen(str) + 1];
    while (str[i]) 
    { 
        c = str[i]; 
        c = tolower(c);
        strcat(cstr, &c); 
        i++; 
    } 

    cout << cstr << endl; 
    return 0; 
}

Hi so I am trying to take a cstring and make it lowercase, but when I am printing the cstring at the end I am getting a weird format box where some of the letters should be. Do anyone have any ideas?

#include <string>
#include <iostream>
#include <string.h>

using namespace std;

int main () 
{ 
    int i=0; 
    char* str="TEST"; 
    char c; 
    char* cstr = new char[strlen(str) + 1];
    while (str[i]) 
    { 
        c = str[i]; 
        c = tolower(c);
        strcat(cstr, &c); 
        i++; 
    } 

    cout << cstr << endl; 
    return 0; 
}

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

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

发布评论

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

评论(4

无法言说的痛 2024-11-09 19:28:10

问题是您错误地调用了 strcat。第二个参数不是以 null 结尾的字符串。

您实际上根本不需要调用 strcat 。只需直接写入输出字符串:

尝试:

  while (str[i])
  {
    c = str[i];
    c = tolower(c);
    cstr[i] = c;
        i++;
  }
  cstr[i] = 0;

或者等效的:

while(str[i])
{
  cstr[i] = tolower(str[i]);
  i++;
}
cstr[i] = 0;

The problem is that you are calling strcat incorrectly. The second parameter is not a null-terminated string.

You really don't need to call strcat at all. Just write directly to the output string:

Try:

  while (str[i])
  {
    c = str[i];
    c = tolower(c);
    cstr[i] = c;
        i++;
  }
  cstr[i] = 0;

or, equivalently:

while(str[i])
{
  cstr[i] = tolower(str[i]);
  i++;
}
cstr[i] = 0;
惟欲睡 2024-11-09 19:28:10

strcat 需要一个空终止 char*,因此通过给出本地 char 的地址,您将调用 < a href="http://en.wikipedia.org/wiki/Undefined_behavior" rel="nofollow">未定义行为。

此外,new char[std::strlen(str) + 1] 不会将数组初始化为 0,这意味着 cstr 不会要么正确地以空值终止;将 () 添加到 new[] 会导致数组被值初始化。

试试这个:

#include <cstddef>
#include <cctype>
#include <cstring>
#include <ostream>
#include <iostream>

int main()
{
    char const* str = "TEST";
    char c[2] = { };
    char* cstr = new char[std::strlen(str) + 1]();
    std::size_t i = 0;
    while (str[i])
    {
        c[0] = static_cast<char>(std::tolower(str[i++]));
        std::strcat(cstr, c);
    }
    std::cout << cstr << std::endl;
    delete [] cstr;
}

strcat expects a null-terminated char*, so by giving the address of a local char you are invoking undefined behavior.

Additionally, new char[std::strlen(str) + 1] does not initialize the array to 0s, meaning cstr won't be properly null-terminated either; adding () to the new[] causes the array to be value-initialized.

Try this instead:

#include <cstddef>
#include <cctype>
#include <cstring>
#include <ostream>
#include <iostream>

int main()
{
    char const* str = "TEST";
    char c[2] = { };
    char* cstr = new char[std::strlen(str) + 1]();
    std::size_t i = 0;
    while (str[i])
    {
        c[0] = static_cast<char>(std::tolower(str[i++]));
        std::strcat(cstr, c);
    }
    std::cout << cstr << std::endl;
    delete [] cstr;
}
谁许谁一生繁华 2024-11-09 19:28:10

strcat 的第二个参数应该是一个以 null 结尾的字符串,而不是单个字符的地址。 strcat 不适合这种用途。

int main ()
{
    const char* str="TEST";
    char* cstr = new char[strlen(str) + 1];
    cstr[strlen(str)] = 0;
    for (int i = 0; str[i]; ++i) {
        cstr[i] = tolower(str[i]);
    }
    cout << cstr << endl;
}

The second argument of strcat is supposed to be a null terminated string, not the address of a single character. strcat isn't appropriate for this use.

int main ()
{
    const char* str="TEST";
    char* cstr = new char[strlen(str) + 1];
    cstr[strlen(str)] = 0;
    for (int i = 0; str[i]; ++i) {
        cstr[i] = tolower(str[i]);
    }
    cout << cstr << endl;
}
缘字诀 2024-11-09 19:28:10
#include <cstddef>
#include <cctype>
#include <cstring>
#include <ostream>
#include <iostream>

#include <string>

int main()
{
    std::string str = "TEST";
    std::string cstr;

    for (std::string::const_iterator it = str.begin(); it!= str.end(); ++it)
        cstr.push_back(tolower(*it));

    std::cout << cstr << std::endl;
}

或者更短:

#include <algorithm>
#include <iterator>

...

    std::transform(str.begin(), str.end(), std::back_inserter(cstr), tolower);
#include <cstddef>
#include <cctype>
#include <cstring>
#include <ostream>
#include <iostream>

#include <string>

int main()
{
    std::string str = "TEST";
    std::string cstr;

    for (std::string::const_iterator it = str.begin(); it!= str.end(); ++it)
        cstr.push_back(tolower(*it));

    std::cout << cstr << std::endl;
}

Or even shorter:

#include <algorithm>
#include <iterator>

...

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