#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
char input[] = "Hello!";
for(int i=strlen(input);i>=0;i--){
input[i+2] = input[i];
}
input[1] = 'i';
input[0] = 'H';
cout<<input<<endl;
return 0;
}
I tried this sample program to shift the string by 2 places and appended a Hi infront. The code works perfectly fine. Could someone please explain how the for loop runs i.e. how does the shifting take place? I made an array of 6 elements and later I am accessing the 8th index, still it works fine. How come ?