Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 9 – Lesson 1 Arrays – Character Strings

Similar presentations


Presentation on theme: "Week 9 – Lesson 1 Arrays – Character Strings"— Presentation transcript:

1 Week 9 – Lesson 1 Arrays – Character Strings
IPC144 Week 9 – Lesson 1 Arrays – Character Strings

2 Arrays As was mentioned in the previous slide show, an array in C, is a single variable that can store individual pieces of data of the same type. numbers shoeSize ages

3 Strings So far, we have learned how to store data such as integers in an array, but we can also store strings of text or character strings. Think of character strings as: an array of characters. Example: each character is an element of the array word word 1 2 3 4

4 Strings Actually, for character strings, there is one additional element which is used to mark the “end of the string”. A delimiter is a symbol to separate objects or fields. For character strings, you need a delimiter to specify the end of the character. This will prevent any confusion when changing or manipulating character strings later in your program. You will also be learning about delimiters when you learn to read and write to text files later in this course...

5 Strings In C programming, the character to represent the end of the character string is the NULL string. The symbol \0 (backslash + zero) is used to represent the NULL string. The NULL string cannot be displayed on the screen since it is NULL or EMPTY. Therefore, this delimiter is useful to mark the end of strings, and yet not be displayed with the character string's contents...

6 Strings It is important for C programming students to be aware that a proper character string in C should end with a NULL character (\0). Why? Because in programming you will be learning how to change or manipulate character strings, which means you may be making them larger or smaller, and if you fail to add the NULL character to delimit your character string, your program may not operate correctly!

7 Declaring Character Strings
We have already learned how to declare arrays. In the case of character strings, we simply declare an array of characters. But there is a WARNING! Since the NULL character \0 needs to be added to the end of the character as a delimiter, then the size of the array should take into account that extra character eg. #define SIZE char word[SIZE + 1]; This protects the program in case the user enters a character string that is 80 characters (81 characters because of NULL character!)‏

8 Declaring Character Strings
To keep consistent in coding with arrays using character strings, you can define the constant and add 1 eg. #define SIZE char word[SIZE];

9 Initializing Character Strings
There are two methods of assigning character strings to arrays during initialization: 1. Assign by character (need to include \0) during array declaration eg. char word[SIZE+1] = { 'h','e','l', 'p', '\0' }; 2. Assign entire string during array declaration (\0 is automatically added to end of string)‏ eg. char word[SIZE+1] = “help”;

10 Assigning Values (After Declaration)‏
There are really only two ways to assign values AFTER a character array has been declared: 1. Use a for loop to change the value of each element. (i.e. each character at-a-time)‏ 2. Use a string function to make a copy of a character string to an array (strcpy function)‏ You CANNOT simply use the following: word = “hello”; <- this will not compile!

11 String Functions Instead of re-writing common functions involving strings such as determining length, copying and comparing character strings, many have been included in the string.h C library. To use these functions in your program, you add the following include statement: #include<string.h> 1. Determine number of characters or length: strlen() Copy character strings to arrays: strcpy() Compare character strings: strcmp()‏

12 String Functions - strlen()‏
Many on the string functions use the \0 character for reference. The strlen() function is passed a string, and returns a number indicating how many characters are stored in the string. eg. numberOfCharacters = strlen(word1); Actually, it is a simple program to create yourself using a loop and logic to return the index number that contains the \0 character. But since this is a common function, it has been included in the string.h C library for convenience!

13 String Functions - strcpy()‏
The strcpy() function takes two strings, and copies the second string into the first string. You can use strcpy() function to copy among arrays, or copy a string within double-quotes to an array. eg. strcpy (array1, array 2); strcpy (word, “greetings”); WARNING: Do not copy a string that has a greater capacity of the array you are copying to!

14 String Functions - strcmp()‏
The strcmp() function takes two strings, and returns a number indicating the size of the first string relative to the second string. eg strcmp(string1, string2); Possible Return Values: negative integer – string 1 is smaller than string 2 positive integer – string 1 is greater than string 2 zero (0) – string 1 is equal to string 2

15 Character Functions toupper()‏ - Converts lowercase character to UPPERCASE character. Takes no action if character is already uppercase. tolower()‏ - Converts UPPERCASE character to lowercase character. Takes no action if character is already lowercase. toupper() / tolower() functions only work on one character at-a-time, so you would need to use a loop to convert case of the entire array's contents...

16 Common Problems Involving Character Strings
Failing to set SIZE of array 1 character more than maximum character string length. Failing to set \0 character when initializing array of characters, one character at a time. Failing to take advantage of character string functions or add #include<string.h> C library in program. Failing to properly assign values to array after declaration, or forgetting to add \0 character at end of character string if manipulating character-by-character (i.e. using for loop).

17 Just for Fun... Character String manipulation is very important, and you will be required to perform a lot of it in future courses. Write a C Program called string_9.c that assigns a character string to an array, and have it remove leading white space characters before the first character. You can assume white space in this case is a space “ “ Use two functions called displayString() and removeWhiteSpace() in your program...

18 Just for Fun... Here is an example of how the program should work:
cc string_9.c a.out x - I want word to line up under the x Seneca college string with leading spaces modified string

19 Additional Resources Here are some Related-Links for Interest Only:


Download ppt "Week 9 – Lesson 1 Arrays – Character Strings"

Similar presentations


Ads by Google