+ 1
[SOLVED] How can i solve this task? Most logical solution what i thought about was the code I'll link below
Here is the task: Task: Create a program that replaces each letter in a message with its corresponding letter in a backwards version of the English alphabet. Input Format: A string of your message in its normal form. Output Format: A string of your message once you have encoded it (all lower case). Sample Input: Hello World Sample Output: svool dliow
13 Respostas
+ 3
The following is another way to solve it but i can't say if it's an efficient way to do so.
string str="hello world";
string new_str="";
foreach(char i in str){
if(i>='A' && i<='Z'){
new_str+=(char)(65-(int)i+90);
}
else if(i>=97 && i<=122){
new_str+=(char)(97-(int)i+122);
}
else new_str+=i;
}
Console.WriteLine(new_str.ToLower());
+ 2
Abhay it would be much more efficient if instead of creating a new string for each char (even concatenation like this will create a new string) you just output the char for each iteration of the loop, without '\n' of course.
But you need to change it to use lowercase alphabetic characters instead.
While the OP's code works it can be greatly improved using your code with the modifications I stated, both in time and space complexity .
+ 2
ChaoticDawg thank you, i really forgot that i was creating a new string for each char!!
+ 1
you should have the problem for all not letter characters...
fix:
if (0<=x) inp[i] = aRev[x];
+ 1
what if user input is "Hello, world"?
+ 1
Ew😳😳 I wasn't expected that😳😳
+ 1
nothing in the task description tell you that input will be composed only by spaces and letters ;P
+ 1
:o
wow...
do you want to add at least about 230 characters? (and thousands if you want to support unicode ^^)
0
Ouch, i just solved it😳😳 For everyone that didn't get - there wasnt a 'space' char in my 'alphabet' array😳
0
visph
nah, just the thing was that my 'alphabet' have no 'space' value and when user writes few words, my code brakes. I think that's cuz i use 'IndexOf' method. And this method cant find 'space' char in my array
0
I see thats easy to fix😳 Just adding needed chars at the end of both arrays👍🏿
0
Its just SoloLearn task, so i dont need to😃😃 But if I'd wanted to do it, i actually dunno what other way can i choose to solve it😳