PROBLEM LINK:
Practice
Setter: Charan Narukulla
Tester: Abhilash Movva
DIFFICULTY:
Cake-Walk
PREREQUISITES:
Stack
PROBLEM:
Given a statement, In that
1.Any space should be replaced by &.
2.If the alphabet is not A/B/C/D/E then they should appear at the end in reverse order. For example, BESTFRIENDBESTFRIEND will become BEEDNIRFTSBEEDNIRFTS.
EXPLANATION:
Take the line as input and use a string to store the answer.
Let ans be the answer string.go through the input string ,Push the char to the stack if it’s not A/B/C/D/E. if it is A/B/C/D/E concatenate it to the ans. If it is a space concatenate & to ans.
Now print the string ans first and pop out all the elements of the stack.
Setter's Solution
int a=-1;
char sstack[200];
void push(char s){
sstack[++a]=s;
}
void dis(){
for(int i=a;i>=0;i--)
std::cout << sstack[i];
}
int main() {
// your code goes here
int T;
cin>>T;
bool ign=1;
while(T–){
string S;
if(ign)
cin.ignore();
std::getline (std::cin,S);
for (int i=0;i<S.length();i++){
if(S[i]==' ')
cout<<"&";
else if(S[i]=='A' ||S[i]=='B' ||S[i]=='C' ||S[i]=='D' ||S[i]=='E')
cout<<S[i];
else
push(S[i]);
}
dis();
a=-1;
ign=0;
std::cout << std::endl;
}
return 0;
}
Tester's solution
/* encode kalakeya */
stack s;
int main(){
int t;
cin >> t;
while(t–){
char str[150];
scanf(" %[^\n]s",str); //scanset working
for(int i=0; i<strlen(str); i++){
if(str[i]=='A' || str[i]=='a' || str[i]=='B' || str[i]=='b' || str[i]=='C' || str[i]=='c'|| str[i]=='D' || str[i]=='d' || str[i]=='E' || str[i]=='e' ){
cout << str[i];
}
else if(str[i]==' '){
cout << "&";
}
else{
char t = str[i];
s.push(t);
}
}
while(!s.empty()){
cout << s.top();
s.pop();
}
cout << endl;
}
return 0;
}