New course! Every coder should learn Generative AI!
Try a free lesson+ 5
I have a situation with my "The spy life" code coach solution
Reff: 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 Problem: My code works fine for the test cases, but don't get approved. my code: #include <iostream> ...(in the comments, thanks)
15 Answers
0
Emeka
"I made it so to initialize the length of rev, so as to make changes to it "--sry I don't understood it.
I about, your rev string length should be less than the original input string. Because you have to remove non-alphabets. Setting a NULL will not remove characters. So you have characters but invisible, I think this is causing the problem. Because it checks output with expected output lexicographically...
Instead setting null, add only those which are alphabets to rev by rev+=wrd[s]; will solve the problem easily I think. No inbuilts used also..
Hope it helps..
+ 3
#include <iostream>
using namespace std;
int main()
{
int a, b, i, j = 0;
string wrd, rev;
getline(cin, wrd);
rev = wrd;
for (i = 0; wrd[i] != '\0'; i++)
{
a = i;
}
for (b = 0; b < i; b++)
{
rev[b] = wrd[a];
a--;
}
for (int s = 0; s < rev.length(); s++)
{
for (char c = 'a'; c <= 'z'; c++)
{
if (rev[s] == c)
{
j = 1;
}
}
if (j != 1)
{
if (rev[s] == ' ')
{
rev[s] = ' ';
}
else
{
rev[s] = NULL;
}
}
j = 0;
}
cout << rev;
return 0;
}
what do you think is possibly wrong?
+ 3
Jayakrishna🇮🇳 , I made it so to initialize the length of rev, so as to make changes to it
+ 3
Emeka You should also check for * symbol and remove code that does nothing
+ 3
Ilyas Bakirov , I appreciate the fact that you used inbuilt libraries. I believe this is sololearn trying to curb my approach of "writing from scratch"😂. Thanks... though I'm still open to knowing "what's really happening" so: incase of next time
Jayakrishna🇮🇳 , thanks.
+ 3
Jayakrishna🇮🇳 https://code.sololearn.com/cwra9d4UaP57/?ref=app this was the initial idea, but I didn't want to have to create another variable lol. So I modified existing one instead. thanks 🤝🏼
UPDATE(III)
+ 3
Emeka Yes, your code longer and I'm glad you succeeded 👍
As Jayakrishna🇮🇳 said your code duplicates itself for upper case letters and it's OK for the beginning 😜
Coding must go smoothly and with no hard, so try to simplify things and code where you see and can do, by the way built-in functions are not evils 👹
+ 2
I got two warnings, so I made little changes: https://code.sololearn.com/c5Pz001i3fxn/?ref=app
UPDATE(II)
+ 2
Syed huzef Ali Your code from different task 🙂
+ 1
I think, your rev length is same as wrd length.. but it should be less. so you have invisible charectets in rev.. may this is the problem...
Emeka
instead use,
string rev="";
rev+=wrd[s]
it can be improved much... try again.
hope it helps..
+ 1
Emeka What are you achieve with first for loop where you assign a = i?
Can you place your code in Code Playground?
+ 1
Ilyas Bakirov it's actually for a character array case(word reverse), cause we don't know how long the real elements are. I changed it to string and forgot to use .length() instead
+ 1
Emeka
Except the warnings this should work I think... Is it?
And just for info, may be you know it!
for (char c = 'a'; c <= 'z'; c++)
{
if (rev[s] == c)
{
filtered+=rev[s];
}
}
Can be rewrite without loop, simply:
if( rev[s] >= 'a' && rev[s]<='z')
filtered += rev[s];
for capital and space also be included in this single if condition..
You're welcome..
- 1
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
char x[7]={'!','@','#','#x27;,'%','&','*'};
int i=7;
cin>>i;
for(i=1;i<=7;i++){
if("password passes the minimum 2 numbers") cout<<"strong"<<i<<endl;
cout<<"hello@$world19"<<endl;
}
else {
cout<<"weak";
}
return 0;
}