Reverse the string

Description:

Reverse each word in a sentence Test Case 1 Input (stdin) great programmers Expected Output taerg sremmargorp Test Case 2 Input (stdin) wonderful work Expected Output lufrednow krow

Program :

#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;




int main() {
 // your code goes here
    string str;

    int i,index,temp,n,j;
    //cin>>n;
    //cin.ignore();
   // for(j=0;j<n;j++){
  getline(cin,str);
     int k=0;
    for(i=0;i<=str.size();i++){
            if(!isalnum(str[i])){
                for(j=i-1;j>=k;j--){
                    cout<<str[j];
                }
                if(i!=str.size())
                 cout<<str[i];
                k=i+1;
            }
    }
    //reverse(str3.begin(),str3.end());

    //cout<<str.size()<<str3.size();
 return 0;
}