19)
Program 18 - Strings
Program 18 | Strings | Source code - prog18.c |
Strings are used to store words in a
program. Unlike some other programming languages C
doesn't have a data type for storing strings, all the
data types store numbers only, but as was seen earlier,
characters all have an associated ASCII code which can be
used to store them as a number. By using arrays which
hold the ASCII character codes for words in order, it is
possible to store and manipulate strings. There are a few functions with the prototypes defined in the string.h header file that can be used to make it easier to store and manipulate strings. Three of these functions will be used in the following program, however there are more. To find out information about the others, or any of the C functions in any of the header files, you can use the Miracle-C help. The descriptions are minimal and to the point, but they help. To use the help in Miracle-C select the help menu option, select language/library, near to the bottom of the window there is a link to header files, which can be used to list all the header files that Miracle-C uses ( stdio.h, string.h etc.. ). By clicking on each header file, it will list all the available functions for it. Each one of them can be clicked on to find a brief description of the function. It is worth noting that Miracle-C doesn't have a conio.h header file, instead it has a system.h header file with the same routines in. It is the only C compiler i've come across not to have these in the conio.h header file, but if the line #include <conio.h> is included in the programs, Miracle C doesn't seem to mind, and ignores the fact that it's not there. |
Program code- new parts shown in red |
|
Description of the program code | |||||||||||||||||||
This program is
simple compared to the last few. It shows how a string
can be stored in an array, manipulated, and displayed on
the screen. First of all the #include<string.h> line is new to this program because it is the first that uses strings and there are some functions defined in there that will be used in the program. An array of 50 unsigned char's is declared called name, which is local to the main function. This allocates the storage in memory needed to hold a string with a maximum length of 49 characters. The reason it will only hold upto 49 characters and not 50 is because C uses "NULL terminated strings". The NULL is a special character with ASCII code 0 that is added to the end of the string. So for example, the string Program would be stored as the following in an array.
Page 9 contains the ASCII character set which can be used to see the corresponding ASCII codes and characters. The NULL (0) at the end of the string tells the C string routines where the string ends. Therefore, if an array of 50 characters is declared, it can only hold upto a maximum of 49 characters for the string, and a NULL character. 50 characters aren't needed for this program but it is better to be over than under ! To store a string in an array you cannot do the following : name="Jason"; You have to use the strcpy function. This is shown on the line after the variables are declared : strcpy(name,"Jason "); . This will store the ASCII code for 'J' at array index 0, 'a' at array index 1, 's' at array index 2 and so on... It will also add the NULL character automatically to the end in the array. The next line strcat(name,"Spooner"); isn't necessary, but has been included to show string concatenation, which means to add a string to the end of an existing string. This will overwrite the original NULL character in the array, but add another one at the new end to the string in the array. The reason that the string concatention wasn't really needed for this program is that it would have been a lot easier just to have a strcpy(name,"Jason Spooner"); line, instead of the two that are here, but that wouldn't have shown how concatenation works. To prove it does work, the line printf("My name is %s\n\n",name); has been included with the %s format specifier this time. Note that even though the name variable is an array no index specifiers are used in square brackets on this line, you can just use the array name and the compiler will work out the rest. The final part of the program displays the string as a series of ASCII codes, using a for loop to go through each character in the string. The strlen function is used to obtain the length in characters of the string. This function takes an array as a parameter, and returns back the number of characters that make up the string, excluding the NULL. If you change the line to read for(i=0;i<=strlen(name);i++) , it will show that the NULL character is there when you run the program. With the unmodified line though, the output to the program will look like :
Below is another program that shows how you can store user string input into an array using the scanf function. It will only work with single words though as the scanf function will treat a space character as the end of the string, which is frustrating. There is a function called gets ( "get string" ) which is better suited to getting user string input than scanf. It is defined in the stdio.h header file. See the example in the Miracle-C help for more information.
The only thing to note with this program is the scanf("%s",inword); line. This time the the array variable doesn't have a & in front of it. There is a reason why array variables don't need the & in front and number variables do, but this involves describing pointers, which are a few pages off yet. |
Summary |
This page explained
what strings are, how to store them in arrays, and also
how to manipulate and find the length of them. It also
showed how to use the Miracle-C help system to find out
information about what other functions are available. The
descriptions require a degree of understanding about C
programming, but the general aim of the functions are
sort of explained. It isn't necessary to remember or understand every function in every header file ! most of the functions are very rarely or never used. When programming it's only when you need to do something and can't figure out how that you need to trawl to see if there's a function around to help. After writing a few programs you tend to remember the most useful ones anyway. The next program is another simple program filling in a missing gap that should have been explained when the if command was done. The switch and case command. |
Tasks |
18.1) Write a program that asks the user to enter a single word using the scanf function. The program will then output how many vowels are contained within the word they entered. 18.2) Convert the same program written in 18.1 to use the gets function to allow the user to enter sentences. Use an array with 1000 elements to store it, just to be on the safe side. |
(c) J.C.Spooner 2001