+ 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

16th Feb 2021, 9:07 PM
Nazeekk
Nazeekk - avatar
13 Réponses
+ 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());
16th Feb 2021, 9:37 PM
Abhay
Abhay - avatar
+ 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 .
16th Feb 2021, 9:48 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
ChaoticDawg thank you, i really forgot that i was creating a new string for each char!!
16th Feb 2021, 9:51 PM
Abhay
Abhay - avatar
+ 1
you should have the problem for all not letter characters... fix: if (0<=x) inp[i] = aRev[x];
16th Feb 2021, 9:21 PM
visph
visph - avatar
+ 1
what if user input is "Hello, world"?
16th Feb 2021, 9:26 PM
visph
visph - avatar
+ 1
Ew😳😳 I wasn't expected that😳😳
16th Feb 2021, 9:26 PM
Nazeekk
Nazeekk - avatar
+ 1
nothing in the task description tell you that input will be composed only by spaces and letters ;P
16th Feb 2021, 9:28 PM
visph
visph - avatar
+ 1
:o wow... do you want to add at least about 230 characters? (and thousands if you want to support unicode ^^)
16th Feb 2021, 9:31 PM
visph
visph - avatar
0
Ouch, i just solved it😳😳 For everyone that didn't get - there wasnt a 'space' char in my 'alphabet' array😳
16th Feb 2021, 9:17 PM
Nazeekk
Nazeekk - avatar
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
16th Feb 2021, 9:24 PM
Nazeekk
Nazeekk - avatar
0
I see thats easy to fix😳 Just adding needed chars at the end of both arrays👍🏿
16th Feb 2021, 9:29 PM
Nazeekk
Nazeekk - avatar
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😳
16th Feb 2021, 9:33 PM
Nazeekk
Nazeekk - avatar