Output showing me only the first letter of string
//program to reverse all words except corner words
#include <bits/stdc++.h>
using namespace std;
void printReverse(string str)
{
//print first word
int i = 0;
for (i = 0; i < str.length() && str[i] != ' '; i++)
cout << str[i];
//print middle word
string word = "";
for (i = 0; i < str.length(); i++)
{
if (str[i] != ' ')
word += str[i];
else
{
reverse(word.begin(), word.end());
cout << word << " ";
word = "";
}
}
//print last word
cout << word << " ";
}
int main()
{
string str;
cout << "Enter the string: ";
cin >> str;
printReverse(str);
return 0;
}
Program to reverse all words except corner words. I'm not able to recognize what's wrong. It showed me only the first word of the string and the rest part of the code is working. Please help me.