01-18-2023 10:45 AM
I need to write a program to replace each letter in a string with another letter found in the alphabet at a distance of x letters (x is between -5 and 5 without being able to have the value 0). For example, for x=1 the code is "a", for x=-3 the code is given by the letter x. Can someone help?
Solved! Go to Solution.
01-18-2023 10:57 AM
@P.Dana wrote:
I need to write a program to replace each letter in a string with another letter found in the alphabet at a distance of x letters (x is between -5 and 5 without being able to have the value 0). For example, for x=1 the code is "a", for x=-3 the code is given by the letter x. Can someone help?
We are certainly not going to do your homework for you, but we'd be happy to guide you to a solution that you are already working on. Give it a try and upload your code, even if it doesn't work. You are new to LabVIEW so we won't be too harsh.
01-18-2023 11:04 AM
This is what I have tried so far.
01-18-2023 11:43 AM
What do you know about strings? How does a "string" differ from a "number"? Can you add 3 to a number? Can you add 3 to a "string"? Are you restricting yourself to the letters "a" .. "z" inside the "string"? How can you handle "going off the end" (your example of -3 yields x)?
If you can't answer these questions, you need to learn about Strings. You can try the LabVIEW "Help", or go to the Web and see if a search for "string" brings up something useful ... If all else fails, pay attention in class (be sure to attend!), and ask if something isn't clear.
Bob Schor
01-18-2023 12:21 PM
I don't know much about strings. In class, the teacher went through them very quickly and I couldn't keep up. I told her that I don't know how to do the program, especially the part where z has to return to a. She told me that there would be two options, one in which I would create a loop in which the alphabet would be repeated and another in which I would work with the number of the letter. She recommended the first option, but I don't know exactly how I could do it .
01-18-2023 01:43 PM - edited 01-18-2023 01:50 PM
Hi P,
@P.Dana wrote:
especially the part where z has to return to a. She told me that there would be two options, one in which I would create a loop in which the alphabet would be repeated and another in which I would work with the number of the letter. She recommended the first option, but I don't know exactly how I could do it .
Let's think about that!
So you have an alphabet of "abcdefghijklmnopqrstuvwxyz". Your "code" gives you the corresponding letter: one could use the StringSubset function to pick a char from a lengthy string (constant)! Have you tried that for "code" values from 1 to 26?
Now there is the requirement to also support numbers outside the 1…26 range. How should we do? How can we "adjust" the input value to fit our required range? What about adding 26 to numbers below 1 until they fit into our range? What about subtrating 26 from numbers above 26?
(Hint: did you learn about modulo division in school? For me that was in 2nd class when I learned about division with natural numbers…)
Please start your VI and show us your approach! You need to do your homework on your own when you want to learn something…
@P.Dana wrote:
I need to write a program to replace each letter in a string with another letter found in the alphabet at a distance of x letters (x is between -5 and 5 without being able to have the value 0).
Ah, that old and insecure Caesar Cipher…
Another approach: use a LUT (look-up table)!
Done…
01-18-2023 01:57 PM
So why not just say that you want to implement the Caesar Cypher?
(You also need to specify what should happen with e.g. spaces, decimals, and non-printable characters.)
What I would do:
01-18-2023 02:07 PM
Thank you!
01-18-2023 02:39 PM - edited 01-18-2023 02:47 PM
I recommend to write a clean tester because the cypher is reversible by changing the sign of the shift.
Here's how it could look like.
Of course you must create your own subVI to learn something. Once you have it working, we can compare notes. 😄
(My code is about the size of a large postage stamp. It would be even simpler if we only treat e.g. uppercase characters)