为什么会出现在字符串末尾的问号? [英] Why is there a question mark at the end of the string?

查看:284
本文介绍了为什么会出现在字符串末尾的问号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我弄清楚为什么会出现在我的字符串输出一个问号。我一直对这个简单的扭转一个字符串的做法。我的code能正确地做到这一点。然后我尝试反向字符串存储到一个数组,然后这个数组转换为字符串。因此,这里是有线的东西发生。总是有在该字符串的末尾一个问号。请教我它的原因,以及如何摆脱这个问号。这里是我的code。谢谢你这么多。

 的#include<串GT;
使用命名空间std;诠释的main()
{
    COUT<<请输入一个字符串。<< ENDL;
    字符串str;
    CIN>>海峡;
    INT I = 0;
    INT长度= str.length();
    焦炭ARR [长度]
    // COUT<<长度;
    而(长度!= 0){
        改编[I] = str.at(长度-1);
        长度 - ;
        COUT<<常用3 [I]
        ++我;
    }
    COUT<< ENDL;
    字符串STRI(ARR);
    COUT<<&ENDL LT;<&STRI LT;< ENDL;
    COUT&所述;&下; STRI [4];
    返回0;
}


解决方案

在C(或其它语言)的字符串需要由被终止的'\\ 0' 。毕竟,你不知道你的内存的char * 点如何大的。因此,你需要 的char [长度+ 1] 。此外,变长数组不是C ++的一部分。您需要使用新的[] /删除[] 的malloc()/无()

 的char * ARR =新的char [长度+ 1]; //字符串+'\\ 0'足够的空间
的char [长度] ='\\ 0'; //终止与'\\ 0'串而(长度!= 0){
    // ... //反转
}
COUT<< ENDL;字符串STRI(ARR); //创建新的字符串删除[] ARR; //不要忘记释放内存

不过,如果你是在下降到手动内存分配,你通常缺少标准库的东西。事实上,你可以简单地使用正确构造(它是(4),以下简化的):

 模板<类InputIt>
字符串:: string的(InputIt第一,InputIt最后一个);

和幸运的是,的std ::字符串规定,通过的std ::字符串:: rbegin()的std ::字符串::撕裂()。现在你的code变得轻松了不少:

 的#include<&iostream的GT;
#包括LT&;串GT;诠释的main()
{
    性病::法院LT&;< 请输入字符串。 <<的std :: ENDL;    性病::字符串str;
    给std :: cin>>海峡;    性病::法院LT&;<的std :: ENDL;    //直接创建新的逆转字符串:
    标准::字符串reversed_string(str.rbegin(),str.rend());    性病::法院LT&;< reversed_string<<的std :: ENDL;    返回0;
}

Please help me to figure out why there is a question mark at my string output. I have been working on this simple reverse a string practice. My code can do it correctly. Then I try to store the reversed string into an array and then convert this array to a string. So here is something wired happened. There is always a question mark at the end of that string. Please teach me the reason of it, and how to get rid of this question mark. Here is my code. Thank you so much

#include <string>
using namespace std;

int main()
{
    cout<<"Please enter a string."<<endl;
    string str;
    cin >> str;
    int i=0;
    int length = str.length();
    char arr[length];
    //cout<<length;
    while (length != 0) {
        arr[i] = str.at(length-1);
        length--;
        cout<<arr[i];
        ++i;
    }
    cout<<endl;
    string stri(arr);
    cout<<endl<<stri<<endl;
    cout<<stri[4];
    return 0;
}

解决方案

A string in C (or many other languages) needs to be terminated by a '\0'. After all, you don't know how large the memory your char* points to is. So you need char[length + 1]. Also, variable-length arrays aren't part of C++. You need to use new[]/delete[] or malloc()/free():

char * arr = new char[length + 1]; // enough space for string + '\0'
char[length] = '\0';               // terminate the string with '\0'

while (length != 0) {
    // ...                         // reverse
}
cout << endl;

string stri(arr);                  // create the new string

delete[] arr;                      // don't forget to deallocate the memory

However, if you're dropping down to manual memory allocation, you're usually missing something from the standard library. And indeed, you could simply use the right constructor (it's (4), simplified below):

template <class InputIt>
string::string(InputIt first, InputIt last);

And luckily, std::string provides input iterators that traverse the string backwards via std::string::rbegin() and std::string::rend(). Now your code gets a lot easier:

#include <iostream>
#include <string>

int main()
{
    std::cout << "Please enter a string." << std::endl;

    std::string str;
    std::cin >> str;

    std::cout << std::endl;

    // Create the new reversed string directly:
    std::string reversed_string(str.rbegin(), str.rend());

    std::cout << reversed_string << std::endl;

    return 0;
}

这篇关于为什么会出现在字符串末尾的问号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆