IPC 144 Week 9 Lesson 1 Arrays Character

  • Slides: 19
Download presentation
IPC 144 Week 9 – Lesson 1 Arrays – Character Strings

IPC 144 Week 9 – Lesson 1 Arrays – Character Strings

Arrays As was mentioned in the previous slide show, an array in C, is

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 shoe. Size ages

Strings So far, we have learned how to store data such as integers in

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. each character is an element of the array word Example: word 0 1 2 3 4

Strings Actually, for character strings, there is one additional element which is used to

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. . .

Strings In C programming, the character to represent the end of the character string

Strings In C programming, the character to represent the end of the character string is the NULL string. The symbol (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. . .

Strings It is important for C programming students to be aware that a proper

Strings It is important for C programming students to be aware that a proper character string in C should end with a NULL character (). 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!

Declaring Character Strings We have already learned how to declare arrays. In the case

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 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 80 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!)

Declaring Character Strings To keep consistent in coding with arrays using character strings, you

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

Initializing Character Strings There are two methods of assigning character strings to arrays during

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

Assigning Values (After Declaration) There are really only two ways to assign values AFTER

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!

String Functions Instead of re-writing common functions involving strings such as determining length, copying

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() 2. Copy character strings to arrays: strcpy() 3. Compare character strings: strcmp()

String Functions - strlen() Many on the string functions use the � character for

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

String Functions - strcpy() The strcpy() function takes two strings, and copies the second

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 (array 1, array 2); strcpy (word, “greetings”); WARNING: Do not copy a string that has a greater capacity of the array you are copying to!

String Functions - strcmp() The strcmp() function takes two strings, and returns a number

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(string 1, string 2); 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

Character Functions � toupper() - Converts lowercase character to UPPERCASE character. Takes no action

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. . .

Common Problems Involving Character Strings 1 Failing to set SIZE of array 1 character

Common Problems Involving Character Strings 1 Failing to set SIZE of array 1 character more than maximum character string length. Failing to set 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 character at end of character string if manipulating character-by-character (i. e. using for loop).

Just for Fun. . . Character String manipulation is very important, and you will

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 display. String() and remove. White. Space() in your program. . .

Just for Fun. . . Here is an example of how the program should

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 string with leading spaces Seneca college x - I want word to line up under the x Seneca college modified string

Additional Resources Here are some Related-Links for Interest Only: http: //cs. senecac. on. ca/~ics

Additional Resources Here are some Related-Links for Interest Only: http: //cs. senecac. on. ca/~ics 124/ics 124_datarep_new. html#Ascii. Table http: //cs. senecac. on. ca/~dabesdri/ref/1. 2. html#strings