Want the answer if the code in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 7

Want the answer if the code in C++

The Spy Life +50 XP You are a secret agent, and you receive an encrypted message that needs to be decoded. The code that is being used flips the message backwards and inserts non-alphabetic characters in the message to make it hard to decipher. Task: Create a program that will take the encoded message, flip it around, remove any characters that are not a letter or a space, and output the hidden message. Input Format: A string of characters that represent the encoded message. Output Format: A string of character that represent the intended secret message. Sample Input: d89%l++5r19o7W *o=l645le9H Sample Output: Hello World

12th Jul 2020, 4:50 AM
Rajat Garg
Rajat Garg - avatar
30 Answers
+ 10
Isn't there a "remove" method in the STL library? It's been a while since I've poked around C++, but wouldn't vectors also work here? I'm sure it wouldn't be the most logical approach. I'm still curious though, lol.
12th Jul 2020, 5:28 AM
Fox
Fox - avatar
+ 4
#include<iostream> #include<bits/stdc++.h> int main() { std::string s1; std::cout<<"enter a string"<<std::endl; std::cin>> s1; std::list<char> chars(s1.begin(),s1.end()); for(int i=0;i<chars.size();i++) { if(!(char.isalpha(chars[i]))&&chars[i]!=' ') { std::remove(chars[i]); i-- } std::string s=reverse(s1.begin(),s1.end()) std::cout<<s; } }
12th Jul 2020, 5:09 AM
Rajat Garg
Rajat Garg - avatar
+ 3
Rajat Garg Your code has many errors. 1.Don't use std::remove or std::remove_if.They don't really remove anything,they just rearrange the elements of the container.Use string{}.erase() instead. 2.char is not a class.Thus char.isalpha() will never work.Remove the char. and just use isalpha() 3.Never use a subscripting operator ([ ]) on lists.Lists use forward iterators.Thus chars[i] is wrong.Use a vector instead. 4.reverse() returns a void therefore don't assign its result to anything.Instead assign string s1 to string s1 first then pass its iterators to reverse. s=s1; reverse(s.begin(),s.end()); 5.s does not belong to namespace std!.please remove the std in std::s; In conclusion,I think you really need to brush up on your understanding of C++ before continuing with the challenges
12th Jul 2020, 6:21 AM
Anthony Maina
Anthony Maina - avatar
+ 2
It's Okay.We all have to start from somewhere
12th Jul 2020, 10:21 AM
Anthony Maina
Anthony Maina - avatar
+ 2
for Java import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scan = new Scanner(System.in); char ch; String nString = " "; String txt = scan.nextLine(); for(int i = 0; i<txt.length();i++){ ch = txt.charAt(i); nString = ch + nString; } for(String j: nString.split(" ")){ String tt = j.replaceAll("[^a-zA-Z]", ""); System.out.print(tt+" "); } } }
2nd Feb 2022, 10:35 AM
Avrian Shandy
Avrian Shandy - avatar
+ 1
~ swim ~ I think Fox is correct. There is a remove() function in <algorithm> header, it eliminates all elements in range [first,last) that are equal to a specific value , and returns an iterator to the new end of that range. Here👇 http://www.cplusplus.com/reference/algorithm/remove/
12th Jul 2020, 6:28 AM
Arsenic
Arsenic - avatar
+ 1
in Python whitelist = set('abcdefghijklmnopqrstuvwxy ABCDEFGHIJKLMNOPQRSTUVWXYZ') a=input()[::-1] answer = ''.join(filter(whitelist.__contains__, a)) print(answer)
22nd Aug 2022, 2:41 PM
Rafkhat Nassimullin
Rafkhat Nassimullin - avatar
0
thanks for the help, hope it helps
12th Jul 2020, 5:21 AM
Rajat Garg
Rajat Garg - avatar
0
#include<bits/stdc++.h> using namespace std; int main() { std::string s1; std::string s; std::cout<<"enter a string"<<std::endl; std::cin>> s1; std::list<char> chars(s1.begin(),s1.end()); for(int i=0;i<chars.size();i++) { remove_if(!(char.isalpha(chars[i]))&&chars[i]!="") { i--; } std::s=reverse(s1.begin(),s1.end()); std::cout<<s; } }
12th Jul 2020, 6:08 AM
Rajat Garg
Rajat Garg - avatar
0
#include<iostream> #include<conio.h> #include<string> using namespace std; class abc{ string s; public: void read(void) { cout<<"Enter a string"<<endl; cin>>s; } void reverse(void) { for(int i=s.length()-1;i>=0;i--) { s+=s.at(i); } } void removeSpecialChar(void) { reverse(); for(int i=0;i<s.length();i++) { if(s.at(i)>64 && s.at(i)<=122) { s+=s.at(i); } } } void finalstr(void) { removeSpecialChar(); cout<<"decrpyed code"<<s<<endl; } }; int main() { abc obj; obj.read(); obj.finalstr(); return 0; }
12th Jul 2020, 8:54 AM
Rajat Garg
Rajat Garg - avatar
0
done ❤️
12th Jul 2020, 10:02 AM
Rajat Garg
Rajat Garg - avatar
0
thanks a lot
12th Jul 2020, 10:02 AM
Rajat Garg
Rajat Garg - avatar
0
Anthony Maina , it was first time is was using this concept!
12th Jul 2020, 10:15 AM
Rajat Garg
Rajat Garg - avatar
0
JAVA version, it's works as well 🤗🤗🤗 import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scan = new Scanner(System.in); char ch; String nString = " "; String txt = scan.nextLine(); for(int i = 0; i<txt.length();i++){ ch = txt.charAt(i); nString = ch + nString; } for(String j: nString.split(" ")){ String tt = j.replaceAll("[^a-zA-Z]", ""); System.out.print(tt+" "); } } }
19th Jan 2022, 3:08 AM
Moussa Diallo
Moussa Diallo - avatar
0
The Spy life Program in Python The code [::-1] reverses a string in Python First will create an input num num = input()[::-1] for n in num: #loop through the list if n.isnumeric(): # check if the string contains a number continue else: print(n) # output all strings
19th Oct 2022, 5:22 AM
Sikabalu Puta
0
Rajat Garg where's your code?
12th Nov 2022, 6:09 PM
sen
0
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String txt = scan.nextLine(); String alphanumeric = txt.replaceAll("[^A-Z0-9]", ""); String reversed = new StringBuilder(alphanumeric).reverse().toString(); System.out.println(reversed); } }
24th May 2023, 2:22 PM
Mukul Arora
Mukul Arora - avatar
- 1
error in if statement
12th Jul 2020, 5:09 AM
Rajat Garg
Rajat Garg - avatar
- 1
expected ‘)’ before char
12th Jul 2020, 5:10 AM
Rajat Garg
Rajat Garg - avatar
- 1
and also std::string statement expected ‘)’ before std
12th Jul 2020, 5:11 AM
Rajat Garg
Rajat Garg - avatar