LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do you write a VI that changes letters in a string?

Solved!
Go to solution

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?

0 Kudos
Message 1 of 9
(1,389 Views)

@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.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
Message 2 of 9
(1,381 Views)

This is what I have tried so far.

Download All
0 Kudos
Message 3 of 9
(1,373 Views)

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

Message 4 of 9
(1,291 Views)

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 .

0 Kudos
Message 5 of 9
(1,255 Views)
Solution
Accepted by P.Dana

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)!

  • Put all chars from "a" to "z" into an array of strings.
  • Generate a "replacement alphabet" by shifting all chars by your "code" value! How do you shift/rotate an array? Is there a function able to do that for your?
  • Now iterate over your message text:
    • look up the char value from your alphabet aka find its position in the alphabet.
    • pick the corresponding char from the "replacement alphabet".

Done…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 6 of 9
(1,191 Views)
Solution
Accepted by P.Dana

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:

  • Convert the string to an U8 array
  • Autoindex on a FOR loop,
    • Check lexical class
    • If class is 4 or 5 rotate to new U8 depending on class
  • Autoindex on the output
  • Convert back to string.

 

Message 7 of 9
(1,179 Views)

Thank you!

0 Kudos
Message 8 of 9
(1,172 Views)

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)

 

altenbach_0-1674074293460.png

 

0 Kudos
Message 9 of 9
(1,143 Views)