在反转字符串 'my.name.is' 时,我得到的输出为 'is@.name.my'。我不明白“@”从何而来

while reversing string 'my.name.is' i'm getting output as 'is@.name.my'. I don't understand from where '@' came from

给定一个长度为 S 的字符串,反转整个字符串而不反转其中的单个单词。单词之间用点隔开。

输入: 第一行包含 T,表示测试用例的数量。接下来是 T 个测试用例。每个 case 包含一个包含字符的字符串 S。

输出: 对于每个测试用例,在新的一行中,输出包含反转字符串的一行。

约束条件: 1 <= T <= 100 1 <= |S| <= 2000

示例: 输入:

i.like.this.program.very.much

输出: much.very.program.this.like.i

#include <bits/stdc++.h>
using namespace std;


int main() {
    //code
    int t;cin>>t;
    while(t--) {
        string s;
        cin>>s;
        stack<string> st;
        int siz = s.size();
        char c[siz];
        for(int i =0;i<siz;i++) {
            c[i] = s[i];
        }
        char *token = strtok(c,".");
        while (token != NULL) 
        { 
            st.push(token);
            st.push("."); 
            token = strtok(NULL, "."); 
        }
        st.pop();
        while(!st.empty()) {
            cout<<st.top();
            st.pop();
        }
        cout<<"\n"; 

    }
    return 0;
}

当你

    char c[siz];
    for(int i =0;i<siz;i++) {
        c[i] = s[i];
    }

您没有将 0 附加到 c[] 以标记字符串的结尾。

如果您使用 siz + 1 作为数组大小,并在末尾放置一个零(空字符),它会起作用。

但是,您仍然不应使用 VLA。

    char c[siz];
    c.push(0);
    for(int i =0;i<siz+1;i++) {
        c[i] = s[i];
    }

您可以在字符串末尾添加一个空字符并将字符串大小增加 1 以避免大小冲突错误。