Reversing order of words in a sentence

后端 未结 8 1326
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 02:11
#include 
#include 
using namespace std;
void reverse(char* sentence)
{
    int index = strlen(sentence) - 1;
    char last = \'\\0\';         


        
8条回答
  •  一向
    一向 (楼主)
    2020-12-04 02:45

    Here I used splitting (tokenizing) sentence into words and then use that for reverse printing. Hope this help-

    #include
    #include
    #include
    #include
    using namespace std;
    
    vector split(char* str, char delimiter=' ')
    {
            vector result;
            do
            {
                    char* begin =str;
                    while(*str != delimiter && *str)
                            str++;
                    result.push_back(string(begin, str));
            }while(0 != *str++);
            return result;
    }
    int main()
    {
            string str;
            vector tempStr;
            cout<<"Enter The String: ";
            getline(cin, str);
            cout<=0; i--) str+=tempStr.at(i)+" ";
                    //cout<

提交回复
热议问题