Академический Документы
Профессиональный Документы
Культура Документы
2010‐2011
Laboratory Activity #1
C‐String: Application of Array and Functions
C does not have a primitive String data type. But using the idea that strings are sequence of characters, thus
strings can be stored as array of characters,
char myString1[16] = “My First String”, myString2[20]=”My Second String”;
This representation of string is called C‐String representation.
The length is one important property of strings and it is defined as the number of characters in the given
string. The two C‐String variables above are capable of storing strings with length less than or equal to 15 and 19
respectively both being 1 less than the defined size. This might be a little confusing because it is expected that
the character arrays above can hold up to 16 and 20 character elements respectively. The reason for this is the
presence of a special character, usually the null character ‘\0’, right after the last character of the string when
stored in an array. From the example above the string “My First String” is stored in myString1 this way,
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
M y F i r s t S t r i n g \0
The null character serves as the string terminating character indicating the end of a string. The end of the
string is not necessarily the last index of the array as shown in myString2,
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
M y S e c o n d S t r i n g \0
Sometimes it would be easier, although not memory efficient, if all C‐Strings are represented by arrays of the
same size,
char myString1[20] = “My First String”, myString2[20]=”My Second String”;
If this is the case then this character array can be given a special name using the typedef keyword in C.
typedef char String[20];
String myString1= “My First String”, myString2=”My Second String”;
Notice how naming the character array hides the detail on how strings are stored ‐ another application of
data abstraction. Another benefit is that it also shortens the declaration of variables.
Problem Description
C‐Strings are useful for storing strings but is useless unless functions for access and manipulation are available.
The library string.h offers a wide range of C‐String manipulation functions and is already available in all C
distribution, but for this exercise string.h will not and should not be utilize. In this activity you must provide five
functions for manipulating strings. The descriptions of the functions are as follows:
1. A function that will compute and return for the length of a given C‐String.
2. A function that will convert all the characters of a given C‐String to lower case
3. A function that will convert all the characters of a given C‐String to upper case
4. A function that will reverse a given C‐String
5. A function that will check if given two C‐Strings are equal and return the result. The function should ignore
the casing of characters, e. g. the strings “APple” and “apple” when tested must be equal.
LABORATORY EXERCIES ON DATA STRUCTURES A.Y. 2010‐2011
To test your function, create a main program that will use these functions. The program must be able to
accept two strings as input. The program should have an output similar to this:
In creating the test program only C libraries will be used, thus the iostream.h will not be included and will
be replaced by the stdio.h and conio.h for input and output. Experiment with printf, scanf, getch, getche.
Criteria
80 pts for the functions (16 pts each)
10 pts for the C‐String data type
10 pts for input and output of the main program.
Submission Procedure
1. All the exercises for COSC60 will be saved in a CD‐RW. Since this is the first exercise for this subject,
Open your text editor (e.g. Notepad) and type the following but replacing the italicized words with
your personal information:
[StudentInfo]
Last Name = Your Last Name
First Name = Your First Name
Middle Name = Your Middle Name
Student Number = Your Student Number
Save the file as “cd.inf” in your CD‐RW
2. Create a folder named Exercise1 in your CD‐RW.
3. Save the source code for the first activity inside the Exercise1 folder. Burn the files.
4. Submit the CD‐RW not later than January 6, 2011 6:00 pm.
{For questions please send an email at rlv.cosc60@gmail.com}