Want the answer if the code in C++ | SoloLearn: Learn to code for FREE!

-4

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

c++

7/12/2020 4:50:13 AM

Rajat Garg

29 Answers

New Answer

+19

Where is your try? Where are you stuck? You will not get direct code here. These challenges are given to you for improving your logic and problem solving skill. If you ask for code, what's the point of learning? Share your attempt if you need help.

+6

i-- is missing semicolon( ';') second last line is missing semicolon ';' If you are including bits/stdc++.h header then you don't need to include any other header.

+6

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.

+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

+3

Rajat Garg You are making it complex. You just need to start from the last, if the char is alphabet or a space display it else do nothing.

+2

~ 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/

+2

Arsenic Yeah he is. I am wondering how i messed it up :)

+2

It's Okay.We all have to start from somewhere

+1

#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; } }

+1

thanks for the help, hope it helps

+1

First thing, cin will not read space separated string. Use geline to read the string getline(cin, s); In reverse function, appending to the same string you are trying to reverse? create a temporary string, Append chars to that then replace s with it once the loop is over removeSpecialChar, again appending to the same string you want to remove chars from? Again create a temporary string, attach chars to it and replace s with it once done. wrong processing if(s.at(i)>64 && s.at(i)<=122) char range 92 to 96 are not alphabets simply check if (isalpha(s.at(i)) || isspace(s.at(i)) t += s.at(i);

+1

done ❤️

+1

thanks a lot

+1

Anthony Maina , it was first time is was using this concept!

0

error in if statement

0

expected ‘)’ before char

0

and also std::string statement expected ‘)’ before std

0

https://www.google.co.in/amp/s/www.geeksforgeeks.org/stdremove-stdremove_if-c/amp/

0

remove is there, i think

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; } }