home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CTUTOR1.ZIP / CHAP7.TXT < prev    next >
Encoding:
Text File  |  1986-06-30  |  17.8 KB  |  389 lines

  1.                       Chapter 7 - Strings and Arrays
  2.  
  3.  
  4.                              WHAT IS A STRING?
  5.  
  6.              A  string is a group of characters,  usually letters of 
  7.  
  8.         the  alphabet.   In order to format your printout in such  a 
  9.  
  10.         way that it looks nice, has meaningful titles and names, and 
  11.  
  12.         is  esthetically  pleasing to you and the people  using  the 
  13.  
  14.         output of your program,  you need the ability to output text 
  15.  
  16.         data.  Actually you have already been using strings, because 
  17.  
  18.         the second program in this tutorial,  way back in Chapter 2, 
  19.  
  20.         output a message that was handled internally as a string.  A 
  21.  
  22.         complete  definition  is  a  series  of  "char"  type   data 
  23.  
  24.         terminated by a NULL character, which is a zero. 
  25.  
  26.              When  C  is going to use a string of data in some  way, 
  27.  
  28.         either  to compare it with another,  output it,  copy it  to 
  29.  
  30.         another string,  or whatever, the functions are set up to do 
  31.  
  32.         what they are called to do until a NULL, which is a zero, is 
  33.  
  34.         detected. 
  35.  
  36.                              WHAT IS AN ARRAY?
  37.  
  38.              An array is a series of homogeneous pieces of data that 
  39.  
  40.         are all identical in type, but the type can be quite complex 
  41.  
  42.         as  we will see when we get to the chapter of this  tutorial 
  43.  
  44.         discussing structures.  A string is simply a special case of 
  45.  
  46.         an array. 
  47.  
  48.              The  best way to see these principles is by use  of  an 
  49.  
  50.         example,  so  load  the program CHRSTRG.C and display it  on 
  51.  
  52.         your monitor.   The first thing new is the line that defines 
  53.  
  54.         a "char" type of data entity.  The square brackets define an 
  55.  
  56.         array subscript in C, and in the case of the data definition 
  57.  
  58.         statement,  the  5 in the brackets defines 5 data fields  of 
  59.  
  60.         type  "char" all defined as the variable "name".   In the  C 
  61.  
  62.         language,  all subscripts start at 0 and increase by 1  each 
  63.  
  64.         step  up  to  the  maximum which in  this  case  is  4.   We 
  65.  
  66.         therefore  have  5 "char" type variables  named,  "name[0]", 
  67.  
  68.         "name[1]",  "name[2]",  "name[3]",  and "name[4]".  You must 
  69.  
  70.         keep in mind that in C, the subscripts actually go from 0 to 
  71.  
  72.         one   less  than  the  number  defined  in  the   definition 
  73.  
  74.         statement. 
  75.  
  76.                          HOW DO WE USE THE STRING?
  77.  
  78.              The  variable  "name" is therefore a string  which  can 
  79.  
  80.         hold up to 5 characters, but since we need room for the NULL 
  81.  
  82.         character,  there are actually only four useful  characters.  
  83.  
  84.         To  load  something  useful  into  the  string,  we  have  5 
  85.  
  86.         statements, each of which assigns one alphabetical character 
  87.  
  88.         to one of the string characters.  Finally, the last place in 
  89.  
  90.         the string is filled with the numeral 0 as the end indicator 
  91.  
  92.  
  93.  
  94.                                   Page 42
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.                       Chapter 7 - Strings and Arrays
  105.  
  106.  
  107.         and  the string is complete.   (A "define" would allow us to 
  108.  
  109.         use "NULL" instead of a zero,  and this would add greatly to 
  110.  
  111.         the  clarity of the program.  It would be very obvious  that 
  112.  
  113.         this  was  a  NULL  and not simply a  zero  for  some  other 
  114.  
  115.         purpose.) Now that we have the string,  we will simply print 
  116.  
  117.         it out with some other string data in the output statement. 
  118.  
  119.              The %s is the output definition to output a string  and 
  120.  
  121.         the  system  will output characters starting with the  first 
  122.  
  123.         one in "name" until it comes to the NULL character,  and  it 
  124.  
  125.         will quit.   Notice that in the "printf" statement, only the 
  126.  
  127.         variable  name "name" needs to be given,  with no  subscript 
  128.  
  129.         since  we  are  interested  in starting  at  the  beginning.  
  130.  
  131.         (There  is  actually another reason that only  the  variable 
  132.  
  133.         name  is  given without brackets.   The discussion  of  that 
  134.  
  135.         topic will be given in the next chapter.)
  136.  
  137.                         OUTPUTTING PART OF A STRING
  138.  
  139.              The  next "printf" illustrates that we can  output  any 
  140.  
  141.         single  character of the string by using the "%c" and naming 
  142.  
  143.         the particular character of "name" we want by including  the 
  144.  
  145.         subscript.   The last "printf" illustrates how we can output 
  146.  
  147.         part  of the string by stating the starting point by using a 
  148.  
  149.         subscript.   The & specifies the address of  "name[1]".   We 
  150.  
  151.         will  study this in the next chapter but I thought you would 
  152.  
  153.         benefit from a little glimpse ahead.
  154.  
  155.              This example may make you feel that strings are  rather 
  156.  
  157.         cumbersome  to  use since you have to set up each  character 
  158.  
  159.         one  at  a time.   That is an incorrect  conclusion  because 
  160.  
  161.         strings  are  very easy to use as we will see  in  the  next 
  162.  
  163.         example program. 
  164.  
  165.              Compile and run this program.
  166.  
  167.                           SOME STRING SUBROUTINES
  168.  
  169.              Load  the  example program STRINGS.C for an example  of 
  170.  
  171.         some  ways  to use strings.  First we define  four  strings.  
  172.  
  173.         Next  we  come  to a new function that you  will  find  very 
  174.  
  175.         useful,  the "strcpy" function,  or string copy.   It copies 
  176.  
  177.         from  one  string  to another until it  comes  to  the  NULL 
  178.  
  179.         character.   It is easy to remember which one gets copied to 
  180.  
  181.         which  if  you think of them like an  assignment  statement.  
  182.  
  183.         Thus if you were to say, for example, "x = 23;", the data is 
  184.  
  185.         copied  from  the  right entity to the  left  one.   In  the 
  186.  
  187.         "strcpy"  function,  the data is also copied from the  right 
  188.  
  189.         entity  to  the left,  so that after execution of the  first 
  190.  
  191.         statement,  name1 will contain the string  "Rosalinda",  but 
  192.  
  193.  
  194.  
  195.  
  196.  
  197.                                   Page 43
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.                       Chapter 7 - Strings and Arrays
  208.  
  209.  
  210.         without  the double quotes,  they are the compiler's way  of 
  211.  
  212.         knowing that you are defining a string.
  213.  
  214.              Likewise,  "Zeke"  is copied into "name2" by the second 
  215.  
  216.         statement,  then the "title" is copied.   The title and both 
  217.  
  218.         names are then printed out.   Note that it is not  necessary 
  219.  
  220.         for  the  defined string to be exactly the same size as  the 
  221.  
  222.         string it will be called upon to store,  only that it is  at 
  223.  
  224.         least  as long as the string plus one more character for the 
  225.  
  226.         NULL. 
  227.  
  228.                       ALPHABETICAL SORTING OF STRINGS
  229.  
  230.              The  next function we will look at is the  "strcmp"  or 
  231.  
  232.         the  string  compare function.   It will return a 1  if  the 
  233.  
  234.         first string is larger than the second, zero if they are the 
  235.  
  236.         same  length  and have the same characters,  and -1  if  the 
  237.  
  238.         first  string  is  smaller  than the  second.   One  of  the 
  239.  
  240.         strings,  depending  on the result of the compare is  copied 
  241.  
  242.         into   the   variable  "mixed",   and   the   largest   name 
  243.  
  244.         alphabetically  is  printed  out.   It  should  come  as  no 
  245.  
  246.         surprise   to   you   that  "Zeke"  wins   because   it   is 
  247.  
  248.         alphabetically  larger,  length  doesn't  matter,  only  the 
  249.  
  250.         alphabet.  It might be wise to mention that the result would 
  251.  
  252.         also depend on whether the letters were upper or lower case.  
  253.  
  254.         There are functions available with your C compiler to change 
  255.  
  256.         the  case of a string to all upper or all lower case if  you 
  257.  
  258.         desire.   These  will be used in an example program later in 
  259.  
  260.         this tutorial.
  261.  
  262.                              COMBINING STRINGS
  263.  
  264.              The last four statements have another new feature,  the 
  265.  
  266.         "strcat",  or string concatenation function.   This function 
  267.  
  268.         simply  adds the characters from one string onto the end  of 
  269.  
  270.         another string taking care to adjust the NULL so  everything 
  271.  
  272.         is  still all right.   In this case,  "name1" is copied into 
  273.  
  274.         "mixed",  then two blanks are concatenated to  "mixed",  and 
  275.  
  276.         finally  "name2"  is concatenated to the  combination.   The 
  277.  
  278.         result  is printed out with both names in the  one  variable 
  279.  
  280.         "mixed". 
  281.  
  282.              Strings  are  not difficult and are  extremely  useful.  
  283.  
  284.         You should spend some time getting familiar with them before 
  285.  
  286.         proceeding on to the next topic.
  287.  
  288.              Compile  and run this program and observe  the  results 
  289.  
  290.         for compliance with this definition.
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.                                   Page 44
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.                       Chapter 7 - Strings and Arrays
  308.  
  309.  
  310.                             AN ARRAY OF INTEGERS
  311.  
  312.              Load the file INTARRAY.C and display it on your monitor 
  313.  
  314.         for  an  example of an array of integers.   Notice that  the 
  315.  
  316.         array is defined in much the same way we defined an array of 
  317.  
  318.         char  in  order to do the string manipulations in  the  last 
  319.  
  320.         section.   We  have  12 integer variables to work  with  not 
  321.  
  322.         counting the one named "index".   The names of the variables 
  323.  
  324.         are "values[0]",  "values[1]", ... , and "values[11]".  Next 
  325.  
  326.         we have a loop to assign nonsense, but well defined, data to 
  327.  
  328.         each of the 12 variables,  then print all 12 out. You should 
  329.  
  330.         have  no  trouble following this program,  but be  sure  you 
  331.  
  332.         understand  it.   Compile and run it to see if it does  what 
  333.  
  334.         you expect it to do.
  335.  
  336.                       AN ARRAY OF FLOATING POINT DATA
  337.  
  338.              Load  and display the program named BIGARRAY.C  for  an 
  339.  
  340.         example  of  a program with an array of "float"  type  data.  
  341.  
  342.         This  program has an extra feature to illustrate how strings 
  343.  
  344.         can  be  initialized.    The  first  line  of  the   program 
  345.  
  346.         illustrates to you how to initialize a string of characters.  
  347.  
  348.         Notice  that the square brackets are empty leaving it up  to 
  349.  
  350.         the  compiler  to count the characters and  allocate  enough 
  351.  
  352.         space for our string.   Another string is initialized in the 
  353.  
  354.         body  of the program but it must be declared "static"  here.  
  355.  
  356.         This  prevents  it  from being allocated as  an  "automatic" 
  357.  
  358.         variable and allows it to retain the string once the program 
  359.  
  360.         is started.   There is nothing else new here,  the variables 
  361.  
  362.         are  assigned  nonsense  data and the  results  of  all  the 
  363.  
  364.         nonsense are printed out along with a header.   This program 
  365.  
  366.         should also be easy for you to follow, so study it until you 
  367.  
  368.         are  sure  of what it is doing before going on to  the  next 
  369.  
  370.         topic.
  371.  
  372.                      GETTING DATA BACK FROM A FUNCTION
  373.  
  374.              Back  in chapter 5 when we studied functions,  I hinted 
  375.  
  376.         to you that there was a way to get data back from a function 
  377.  
  378.         by  using  an array,  and that is true.   Load  the  program 
  379.  
  380.         PASSBACK.C for an example of doing that.   In this  program, 
  381.  
  382.         we  define  an array of 20 variables  named  "matrix",  then 
  383.  
  384.         assign  some nonsense data to the variables,  and print  out 
  385.  
  386.         the  first five.   Then we call the function "dosome" taking 
  387.  
  388.         along  the entire array by putting the name of the array  in 
  389.  
  390.         the parentheses.
  391.  
  392.              The  function  "dosome" has a name in  its  parentheses 
  393.  
  394.         also but it prefers to call the array "list".   The function 
  395.  
  396.         needs  to be told that it is really getting an array  passed 
  397.  
  398.         to  it and that the array is of type "int".   The  following 
  399.  
  400.  
  401.  
  402.                                   Page 45
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.                       Chapter 7 - Strings and Arrays
  413.  
  414.  
  415.         line,  prior to the bracket which starts the  program,  does 
  416.  
  417.         that  by  defining  "list" as an integer type  variable  and 
  418.  
  419.         including the square brackets to indicate an array.   It  is 
  420.  
  421.         not  necessary to tell the function how many elements are in 
  422.  
  423.         the  array,  but you could if you so desired.   Generally  a 
  424.  
  425.         function  works with an array until some end-of-data  marker 
  426.  
  427.         is  found,  such  as  a NULL for a  string,  or  some  other 
  428.  
  429.         previously  defined data or pattern.   Many  times,  another 
  430.  
  431.         piece of data is passed to the function with a count of  how 
  432.  
  433.  
  434.         many elements to work with.  In our present illustration, we 
  435.  
  436.         will use a fixed number of elements to keep it simple.
  437.  
  438.              So far nothing is different from the previous functions 
  439.  
  440.         we  have called except that we have passed more data  points 
  441.  
  442.         to  the function this time than we ever have before,  having 
  443.  
  444.         passed 20 integer values.  We print out the first 5 again to 
  445.  
  446.         see if they did indeed get passed here.   Then we add ten to 
  447.  
  448.         each of the elements and print out the new values.   Finally 
  449.  
  450.         we return to the main program and print out the same 5  data 
  451.  
  452.         points.   We  find that we have indeed modified the data  in 
  453.  
  454.         the function,  and when we returned to the main program,  we 
  455.  
  456.         brought  the changes back.   Compile and run this program to 
  457.  
  458.         verify this conclusion.
  459.  
  460.                          ARRAYS PASS DATA BOTH WAYS
  461.  
  462.              We  stated during our study of functions that  when  we 
  463.  
  464.         passed data to a function,  the system made a copy to use in 
  465.  
  466.         the  function which was thrown away when we returned.   This 
  467.  
  468.         is not the case with arrays.   The actual array is passed to 
  469.  
  470.         the  function  and  the function can modify it  any  way  it 
  471.  
  472.         wishes  to.    The  result  of  the  modifications  will  be 
  473.  
  474.         available  back  in  the calling  program.   This  may  seem 
  475.  
  476.         strange  to  you that arrays are  handled  differently  from 
  477.  
  478.         single point data, but they are.  It really does make sense, 
  479.  
  480.         but  you  will  have  to wait until we get  to  pointers  to 
  481.  
  482.         understand it. 
  483.  
  484.                          A HINT AT A FUTURE LESSON
  485.  
  486.              Another way of getting data back from a function to the 
  487.  
  488.         calling program is by using pointers which we will cover  in 
  489.  
  490.         the  next chapter.   When we get there we will find that  an 
  491.  
  492.         array  is in reality a pointer to a list of  values.   Don't 
  493.  
  494.         let  that  worry  you now,  it will make sense when  we  get 
  495.  
  496.         there.  In the meantime concentrate on arrays and understand 
  497.  
  498.         the  basics  of  them because when we get to  the  study  of 
  499.  
  500.         structures  we will be able to define some pretty  elaborate 
  501.  
  502.         arrays.
  503.  
  504.  
  505.  
  506.  
  507.                                   Page 46
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.                       Chapter 7 - Strings and Arrays
  518.  
  519.  
  520.                         MULTIPLY DIMENSIONED ARRAYS
  521.  
  522.              Load  and  display  the file named  MULTIARY.C  for  an 
  523.  
  524.         example  of a program with doubly dimensioned  arrays.   The 
  525.  
  526.         variable "big" is an 8 by 8 array that contains 8 times 8 or 
  527.  
  528.         64 elements total.   The first element is  "big[0][0]",  and 
  529.  
  530.         the last is "big[7][7]".  Another array named "huge" is also 
  531.  
  532.         defined  which  is not square to illustrate that  the  array 
  533.  
  534.         need  not  be square.   Both are filled up  with  data,  one 
  535.  
  536.  
  537.         representing  a  multiplication  table and the  other  being 
  538.  
  539.         formed into an addition table. 
  540.  
  541.              To illustrate that individual elements can be  modified 
  542.  
  543.         at will,  one of the elements of "big" is assigned the value 
  544.  
  545.         from one of the elements of "huge" after being multiplied by 
  546.  
  547.         22.   Next "big[2][2]" is assigned the arbitrary value of 5, 
  548.  
  549.         and  this  value  is  used for the subscripts  of  the  next 
  550.  
  551.         assignment statement.   The third assignment statement is in 
  552.  
  553.         reality  "big[5][5]  = 177" because each of  the  subscripts 
  554.  
  555.         contain the value 5.   This is only done to illustrate  that 
  556.  
  557.         any  valid expression can be used for a subscript.   It must 
  558.  
  559.         only meet two conditions,  it must be an integer (although a 
  560.  
  561.         "char"  will work just as well),  and it must be within  the 
  562.  
  563.         range of the subscript it is being used for.
  564.  
  565.              The  entire matrix variable "big" is printed out  in  a 
  566.  
  567.         square  form so you can check the values to see if they  did 
  568.  
  569.         get set the way you expected them to.
  570.  
  571.         PROGRAMMING EXERCISES
  572.  
  573.         1.   Write  a  program with  three short  strings,  about  6 
  574.  
  575.              characters each, and use "strcpy" to copy "one", "two", 
  576.  
  577.              and  "three" into them.  Concatenate the three  strings 
  578.  
  579.              into one string and print the result out 10 times.
  580.  
  581.         2.   Define  two  integer  arrays,  each 10  elements  long, 
  582.  
  583.              called "array1" and "array2".  Using a loop,  put some 
  584.  
  585.              kind  of  nonsense data in each and add them  term  for 
  586.  
  587.              term  into  another 10 element  array  named  "arrays". 
  588.  
  589.              Finally,  print  all  results in a table with an  index 
  590.  
  591.              number.
  592.              1     2 +  10 =  12
  593.              2     4 +  20 =  24
  594.              3     6 +  30 =  36   etc.
  595.  
  596.              Hint; The print statement will be similar to;
  597.                 printf("%4d %4d + %4d = %4d\n",index,array1[index],
  598.                         array2[index],arrays[index]);
  599.  
  600.  
  601.  
  602.                                   Page 47
  603.  
  604.