- 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
30 Respostas
+ 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.
+ 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;
}
}
+ 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
+ 2
It's Okay.We all have to start from somewhere
+ 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+" ");
}
}
}
+ 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/
+ 1
in Python
whitelist = set('abcdefghijklmnopqrstuvwxy ABCDEFGHIJKLMNOPQRSTUVWXYZ')
a=input()[::-1]
answer = ''.join(filter(whitelist.__contains__, a))
print(answer)
0
thanks for the help, hope it helps
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;
}
}
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;
}
0
done ❤️
0
thanks a lot
0
Anthony Maina , it was first time is was using this concept!
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+" ");
}
}
}
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
0
Rajat Garg where's your code?
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);
}
}
- 1
error in if statement
- 1
expected ‘)’ before char
- 1
and also std::string statement
expected ‘)’ before std