programming: i want to take an input of a string but seperate the characters

Disclaimer: I am in a Computer Science class, but it’s not as if this is the answer to a homework problem. It is a general question.

I understand that this is probably an extremely simple thing to do. I am using ADA, and specific advice would be great, but general programming advice will be fine too, as I feel once I get this concept, I can apply it myself.

So I want to take an input of a string of characters (letters or numbers, but in this case numbers), and be able to manipulate the characters seperately (ie store each one to a seperate variable).

The approach I think might work is defining the variables as character type and calling a text_io.get for each character one after another, without any puts or new lines. Unfortunately, I have been unable to configure gnat-gps on my home computer, so I can not test this theory right now.

To clarify, my code might look something like this:



First : Character
Second : Character
Third : Character

BEGIN

Ada.Text_IO.Put("Enter a three digit number");
Ada.Integer_Text_IO.Get(item => First);
Ada.Interger_Text_IO.Get(item => Second)


etc…

Any specific ADA or general object-oriented programming advice would be much appreciated.

It’s probably not a very good idea to stuff each character in its own variable. Remember that a string is an array of characters and you can index into an array as long as you know how long it is. For example, I think array references in Ada look like array(0) to get the first element, array(1) to get the second, and so on. Using that method is infinitely more idiomatic than having a different variable for each character entered.

(BTW, what OS are you using? I might be able to help you get GNAT working.)

Wow, thanks for the prompt response, and that sounds like an incredibly more efficient way to do it. To clarify, if i saved the string in a variable called String, I could simply reference, say,

String(2) + String(0);

?
Oh, and I am running Ubuntu 8.04

If you want to add two characters, yes. I don’t know why you would want to do that.

Then type ‘sudo apt-get install gnat-gps’ into a command prompt (and probably ‘sudo apt-get install gnat-gps-doc’ as well). Type ‘y’ if it asks you if you want to install all those other packages: You need to in order to get what you want. Get some coffee while it all downloads and configures, and when the machine stops churning and the command prompt returns you’re done. (Damn, I love package repos. :D)

Note that I don’t know ADA.

String(2) + String(0) might work, but don’t be too surprised if it doesn’t.

Firstly, given two characters, what does the + operator do? Will it treat them as numbers and add them or will it combine them? If it combines them, will the result be printable without a null-byte at the end?

Secondly, are your characters single bytes or multiple bytes? If they are multiple bytes, then you need to know whether accessing the string as an array in ADA will skip that many characters ahead or that many bytes.

Ultimately, you’re safest to check if ADA has any functions for dealing with strings, like a substring function or a concatenate function. These will generally solve such issues for you without you having to understand how the strings are being represented by the system internally.

So even though you can access the characters as an array or split them up into separate variables, you are best not to.

Yes, the example i was thinking of had to do with the checksum on an ISBN number

phenomenal, gps works now! thanks for all your help.

I’m not very familiar with Ada, but I don’t think you can do arithmetic on characters like that. (And as Derleth suggests, adding characters is a strange thing to want to do.) Some languages like C will let you do it, but others insist on treating characters as a non-numeric type.

Apparently the CHARACTER’POS operator/function in Ada will convert a character to its ASCII code as an integer. An example of using it can be found here. That also shows the CHARACTER’VAL function, which goes in the opposite direction.

Of course I could be wrong, and you can just go ahead and try it. What’s the worst that could happen?

There is a very good and complete book about Ada on Wikibooks. Apparently, I forgot a lot more than I realized and some of what I said might be wrong. (For one thing, arrays can start at 1 and not 0, meaning the first character would be string(1) instead of string(0).) Here is the section specific to strings.

Additionally, there is a complete Reference Manual available online. It looks like it would answer any possible question you might have.

I don’t know anything about ADA, but in some other languages that I have worked with that have a function called atoi(ascii to integer). Normally, this will take a string as an input and return an integer with the value of any number found in the string. For example, if a string is ‘abc123’, using atoi(‘abc123’) would return an integer with the value of 123. Then you can use mod math* and get the individual digits out that way.

*mod math is to take the number 123 and divide it by 100. Put the dividend in one variable and the remainder in another. Take the remainder and divide it by 10. put the dividend in one variable and the remainder in another. The remainder from the last operation will be the one’s digit, the first dividend will be the hundred’s digit, and the second dividend will be the ten’s digit.

Mines Mystique

This is the method I have been using, but it is rather unwieldy and was hoping for something a little more convenient.

I have not analyzed this thread in detail but I used to teach Ada for a living. But that was in 1989-90. I taught Ada86 and have a passing familiarity with Ada93 (is that the right year?). I am a little rusty.

The + operator is concatenation for characters and strings. Do not confuse this with C-ish attempts to do arithmetic on characters.

A String is technically not an array, it’s a distinct data type (BTW you would not be able to name a variable String). The syntax is just like an array, and it’s stored that way, but if you’re a language lawyer it’s not an array.

You can also reference slices in Ada, such as s(4…6) (I think that’s the correct syntax) for characters 4-6 in the string s, instead of having to use a substring function.

(Also, Ada is not an acronym so it’s not all caps. It is named after Lord Byron’s daughter Ada Lovelace who was an assistant to Charles Babbage, inventor of an early (first?) mechanical computing machine, the Difference Engine. One can make the tortured claim that she was the first programmer.)

the current version is Ada95, just for the record.

Referencing slices seems to be what i want. off to the sandbox to try!

I must correct myself. A String is a predefined data type that is indeed an array of Characters. It may be indexed with positive numbers.

In Ada there is a difference between a predefined data type and a built-in data type. A predefined type is built from atomic language elements but you don’t see the actual code that defines it, unlike library elements in a language like Java.