简体   繁体   English

字符串中的反词

[英]Reverse word in a string

This code here is for reversing words in a string. 此处的代码用于反转字符串中的单词。 The problem is that it only reverses the first word in the string. 问题在于它仅反转字符串中的第一个单词。 When I ran a trace I found that it is stopping after encountering the statement if(s[indexCount] == '\\0') break; 当我运行跟踪时,我发现它在遇到if(s[indexCount] == '\\0') break;语句后正在停止if(s[indexCount] == '\\0') break;

Why the code is getting null character every time the first word is reversed even though some other character is present after the first word. 为什么即使第一个单词之后出现其他字符,但每次第一个单词反转时代码都变为空字符。

#include <iostream>
using namespace std;
int main()
{
    string s;
    char tchar;
    int indexCount=0,charCount=0,wordIndex;
    cin>>s;
    while(1){
        if(s[indexCount]==' ' && charCount==0) continue;
        if(s[indexCount]==' ' || s[indexCount]=='\0' ){
            wordIndex=indexCount-charCount;
            charCount=indexCount-1;
            while(charCount!=wordIndex && charCount>wordIndex){
                tchar=s[wordIndex];
                s[wordIndex]=s[charCount];
                s[charCount]=tchar;
                charCount--;
                wordIndex++;
            }
            if(s[indexCount] == '\0') break;
            indexCount++; charCount=0;
        }
        else{
            charCount++;
            indexCount++;
        }
    }
    cout<<"\nReveresed words in the string : \n\t"<<s<<endl;
   return 0;
}

Also I'm using while(1) . 我也在使用while(1) Does it make this a bad code? 这会使它成为错误的代码吗?

The problem indeed lies with the method of input. 问题确实在于输入方法。 cin >> string_variable will consider whitespace to be a delimiter. cin >> string_variable将空白视为定界符。 That is why only the first word is being entered. 这就是为什么只输入第一个单词的原因。 Replace cin >> s; 替换cin >> s; with getline(cin, s); getline(cin, s); and it will work correctly. 它会正常工作。

First of all I want to point out that 首先我要指出

cin >> stringObject;

will never ever read space character! 永远不会读空格字符! so inserting My name is geeksoul will cause above code to read only My and leave everything else in the buffer! 因此,插入“ My name is geeksoul将导致上面的代码仅读取“ My并将其他所有内容保留在缓冲区中!

To read space character you should use getline function like this 要读取空格字符,您应使用像这样的getline函数

std::getline(std::cin, stringObject);

read about getline 了解有关getline的信息

Second The standard doesn't say that in case of an std::string '\\0' is any special character. Therefore, any compliant implementation of std::string should not treat '\\0' as any special character. Unless of course a const char* is passed to a member function of a string, which is assumed to be null-terminated. Second The standard doesn't say that in case of an std::string '\\0' is any special character. Therefore, any compliant implementation of std::string should not treat '\\0' as any special character. Unless of course a const char* is passed to a member function of a string, which is assumed to be null-terminated. The standard doesn't say that in case of an std::string '\\0' is any special character. Therefore, any compliant implementation of std::string should not treat '\\0' as any special character. Unless of course a const char* is passed to a member function of a string, which is assumed to be null-terminated.

If you really want to check your string with null terminating character then you should consider using stringObject.c_str() which converts your C++ style string to old school C style string! 如果您真的想用空终止符检查字符串,那么应该考虑使用stringObject.c_str() ,它将您的C ++样式字符串转换为旧式C样式字符串!

Check this for c_str 检查此c_str

Finally this might be helpful for you! 最后, 可能对您有所帮助!

Quick tip. 小建议。 If you reverse all characters in the whole strings, and then all characters between each pair of consecutive spaces, you will achieve the same result, with way simple code, like this: (Note, this may not compile or be slightly buggy (haven't compiled or anything), but should convey the basic idea) 如果颠倒整个字符串中的所有字符,然后颠倒每对连续空格之间的所有字符,则将使用简单的代码来实现相同的结果,如下所示:(请注意,这可能无法编译或存在一些错误(避难所t编译或其他任何内容),但应传达基本思想)

void reverseWords(std::string& aString) {
    std::reverse(aString.begin(), aString.end());
    size_t lastSpaceIndex = 0;
    for (size_t index = 0; index != aString.size(); ++index) {
        if (aString[index] == ' ') {
             std::reverse(aString.begin() + lastSpaceIndex + 1, aString.begin() + index);
             lastSpaceIndex = index;
        }
    }
}

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

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