ijsumana
BAN USER1. reverse the whole string.
2. reverse the words in this string
#include <iostream>
#include <new>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;
void reverse(string &s, int st, int end);
int main()
{
string s = "Welcome to Bangladesh";
int i, j = s.length()-1;
i = 0;
//cout<<s<<endl;
reverse(s, i, j);
cout<<s<<endl;
int st, end;
bool started = false;
for(i = 0; i<=j; i++){
if(isalpha(s[i]) && started == false){
started = true;
st = i;
cout<<"s[i] = "<<s[i]<<endl;
}
if(s[i] == ' '){
if(started){
end = i;
reverse(s, st, end-1);
cout<<"temporary after reverse: "<<s<<endl;
started = false;
}
}
}
if(started){
reverse(s, st, i-1);
}
cout<<"Reversed words "<<s<<endl;
return 0;
}
void reverse(string &s, int st, int end){
char temp;
int i, j;
cout<<s<<"st="<<st <<", end= "<<end<<endl;
while(st<end){
temp = s[st];
s[st] = s[end];
s[end] = temp;
st ++;
end --;
}
cout<<s<<endl;
}
in C:
- ijsumana March 02, 2017