0

Given a String of length S, reverse the whole string without reversing the individual words in it. Words are separated by dots.

Input: The first line contains T denoting the number of testcases. T testcases follow. Each case contains a string S containing characters.

Output: For each test case, in a new line, output a single line containing the reversed String.

Constraints: 1 <= T <= 100 1 <= |S| <= 2000

Example: Input:

i.like.this.program.very.much

Output: 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;
}
J...
  • 30,968
  • 6
  • 66
  • 143

2 Answers2

1

When you do

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

you don't append a 0 to c[] to mark the end of the string.

If you used siz + 1 as an array size, and put a zero (null character) at the end, it would work.

But still, you should not use VLAs.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42
0
    char c[siz];
    c.push(0);
    for(int i =0;i<siz+1;i++) {
        c[i] = s[i];
    }

You could add a null character to the end of the string and increase the string size by 1 to avoid size violation errors.