- #1
Ilikecereal
- 17
- 1
Write a program to reverse words of a string individually, for example if you enter: I love C++, it should display I evol ++C
They've given the solution in my textbook but I don't quite understand the logic behind it.
void main( )
{ int l, i, k = 0 ;
char str[80], word[80] ;
cout<<"Enter any string"<<endl ;
gets(str) ;
strcat(str, " ") ;
for(i = 0 ; str !='0' ; i++)
{
if(str != ' ')
{ word[k] = str ;
k = k + 1 ;
}
else
{
while(k > 0)
{ cout<<word[--k] ;
}
cout<<str ;
}
}
getch( ) ;
}
Could someone please explain what they have done here?
They've given the solution in my textbook but I don't quite understand the logic behind it.
void main( )
{ int l, i, k = 0 ;
char str[80], word[80] ;
cout<<"Enter any string"<<endl ;
gets(str) ;
strcat(str, " ") ;
for(i = 0 ; str !='0' ; i++)
{
if(str != ' ')
{ word[k] = str ;
k = k + 1 ;
}
else
{
while(k > 0)
{ cout<<word[--k] ;
}
cout<<str ;
}
}
getch( ) ;
}
Could someone please explain what they have done here?