Reverse a sentence and print each word in reverse order in Javascript
working sample
#include <iostream>
#include<string>
#include <ctype.h>
using namespace std;
string reverse(string &name)
{
string newStr;
int len=name.length()-1;
int track=0;
int count = 0;
for(int i=len;i>=0;i--)
{
int c=name[i];
if (isspace(c))
{
int start=i;
int end=(name.length())-track;
string temp;
for(int i=start;i<end;i++)
{
temp+=name[i];
}
newStr+=temp;
track+=temp.length();
}
}
string dummy="";
for(int i=0;i<name.length();i++)
{
if (isspace(name[i]))
{
break;
}
dummy+=name[i];
}
return newStr+" "+dummy;
}
int main()
{
string name="Hello manish hi";
std::cout << reverse(name) << std::endl;
return 0;
}
Comments
Post a Comment