Why are strings different from numeric arrays and how do
we deal with them?
A string exists only as a sequence of
characters terminated by a special character known as the null
character. The null character is actually an ASCII zero. A
numeric array is a data structure in which contains a fixed number
of contiguous storage elements all of the same data type. In conclusion
a numeric array and string are the same except for two things:
Conclusion: A numeric array and a character array
are the same except for these two
things:
Numeric arrays contain numeric values and not
character
values.
Numeric arrays dont use a terminating null zero
(\0).
Strings only take up 1-byte of storage
per element.
When it comes to string we are only
interested in keeping track of string offsets and how many elements are
contained in a string.
We do not want to count them.
We really don't care much for strings since the
only thing we do with strings is print them out.
Remember we cannot print out numeric values only
symbols.
Things we need to
know.
What Characters are?
How Characters are stored in
RAM?
Why we must convert strings?
How to manipulate
characters.
Where the strings begins?
We deal with strings by assigning a string of
characters as follows:
mystring[0] = 'H'; or chararray[0] =
72;
mystring[1] = 'e';
chararray[1] = 101;
mystring[2] = 'l';
chararray[2] = 108;
mystring[3] = 'l';
chararray[3] = 108;
mystring[4] = 'o';
chararray[4] = 111;
mystring[5] = '\0';
chararray[5] = 0;
Remember strings require 1 additional byte at the
end of the array; the NULL ('/0') character
"Converting Strings to Numbers" pg. 128
"When we enter numbers from the keyboard, we
actually are entering numeric values which are stored as the data type
char. When we read them from the ASCII file, we are actually reading-in
characters. To store them in RAM as numeric values, so we can perform
arithmetic operations on them, we must first convert them to their
numeric equivalents."
First we need to determine if the character belongs
to the set of digits.
As discussed in our slide show as well as class I
am going to go over how to convert.
This is how the code is written
http://www.pkirs.utep.edu/
#include <studio.h>
C Code 5.12.
void main()
{ char nstring[] = "123"; // the
char string we are convert
int num = 0, //
variable num will store the equivalent of nstring
offset = 0; //
the offset/index for our array
while ((nstring[offset] > '0' && (nstring[offset]
< = '9')) //repeat while we can convert
num = num * 10 + (nstring[offset++]
-'0'); // determine number to date }
We know that it has to between '0' and '9'
(inclusive) and if the character value is between 48 and 57,
we know that they are legal values.
The next lines deal with the converting process
described above.
So then we must follow the loop:
offset
|
nstring [offset] |
num=num *10 + (nstring [offset++] - '0') |
0 |
'1' |
1 = 0* 10 + ('1' (=49)-'0' (=48)) |
1 |
'2' |
12 = 1*10 + ('2'(=50) -'0' (=48)) |
2 |
'3' |
123 = 12 *10+('3' (=51) - '0' (=48)) |
3 |
'\0' |
** loop terminated** |
The loop was terminated because remember it stops
once you reach the null character at the end of the string. As well as
in the code the declaration char nstring []="123"; automatically adds a
NULL character at the end of the string.
Remember the basics; if a string consisted of the
characters '54', the corresponding value would be equal to (5*10) + 4,
or 54. If the string was '2633' the numeric equivalent would be (2
*1000) + (6 *100) + (3 *10) + 3 = 2633.
The following are references for additional
information on strings:
http://www.zend.com/zend/tut/using-strings.php#intro
http://www.php.net/manual/en/language.types.string.php
http://www.cplusplus.com/doc/tutorial/tut3-2.html
http://www.cppreference.com/cppstring/
http://www.mhatts.aps.anl.gov/dohn/software/octave/html
http://www.unixhelp.ed.ac.uk/CGl/man-cgi?Strings/html
1. Do you have to use the maximum number of
characters assigned?
No, you do not. Please refer to the following
demonstration.
2. What are the two main differences between a
string and a numeric array?
The two main differences between a string and a
numeric array are:
a) numeric arrays contain numeric values
b) Strings use null to terminate value
3. Null represents what decimal value?
a) 1
b) 2
c) 3
d) 0
(Answer: d) 0)
4. How do we determine the address of an array?
a) base address
b) offset/script
c) base address plus offset/script
(Answer: c) base address plus
offset/script)
5. What does the function atoi do?
a) converts all character to integers
b) allows alphanumeric to integrate.
c) all of the above
d) converts a string to an integer
Answer: d (it converts a string to an integer)
6. Explain what the function ftoa does?
a) converts a string to an integer
b) converts a real number (float) to a string
c) it allow the float to be converted directly to
the base address
d) none of the above
Answer: b (converts a real number (float) to a
string)
|