PLT 101 Computer Programming CHAPTER 5 ARRAYS AND

  • Slides: 27
Download presentation
PLT 101 Computer Programming CHAPTER 5 ARRAYS AND STRINGS (Part 2) Strings

PLT 101 Computer Programming CHAPTER 5 ARRAYS AND STRINGS (Part 2) Strings

Outline • Fundamentals of Characters & Strings • ASCII Character Sets • Declaration of

Outline • Fundamentals of Characters & Strings • ASCII Character Sets • Declaration of Strings • Initialization of Strings • Built-in Function for String Handling • Manipulation of Character & Strings 2

Fundamentals of Characters and Strings � Characters in C consist of any printable or

Fundamentals of Characters and Strings � Characters in C consist of any printable or nonprintable character in the computer’s character set including �lowercase letters & uppercase letters, decimal digits, special characters, escape sequences and whitespaces characters. � A character is usually stored in the computer as an 8 -bits (1 byte) integer. � The integer value stored for a character depends on the character set used by the computer on which the program is running. 3 Which one is greater ? ‘university’ or ‘Uni. MAP’

ASCII Character Set �ASCII Character Set includes definition for 128 characters (0 -127). �Digit

ASCII Character Set �ASCII Character Set includes definition for 128 characters (0 -127). �Digit at the left of the table represent the leftmost digit of decimal number �Digit at the top of the table represent the rightmost digit of decimal number �Example Character ‘F’ is 70 Character ‘&’ is 38 4

ASCII Character Set –cont. 5 0 1 2 3 4 5 6 7 8

ASCII Character Set –cont. 5 0 1 2 3 4 5 6 7 8 9 0 nul soh stx eot enq ack bel bs ht 1 If vt ff cr so si dle dc 1 dc 2 dc 3 2 dc 4 nak syn etb can em sub esc fs gs 3 rs us sp ! “ # $ % & ` 4 ( ) * + , - . / 0 1 5 2 3 4 5 6 7 8 9 : ; 6 < = > ? @ A B C D E 7 F G H I J K L M N O 8 P Q R S T U V W X Y 9 Z [ ] ^ _ ’ a b c 10 d e f g h i j k l m 11 n o p q r s t u v w 12 x y z { | } ~ del

ASCII Character Set –cont. Example u = 117 n = 110 i = 105

ASCII Character Set –cont. Example u = 117 n = 110 i = 105 v = 118 e = 101 r = 114 s = 115 i = 105 t = 116 6 y = 121 U = 85 n = 110 i = 105 Based on decimal comparison: ‘university’ = 1122 ‘Uni. MAP’ = 522 M = 77 A = 65 P = 80 So, ‘university’ is GREATER than ‘Uni. MAP’

Example: ASCII Character Set #include <stdio. h> int main(void) { char my_A = 'A';

Example: ASCII Character Set #include <stdio. h> int main(void) { char my_A = 'A'; char my_Z = 'Z'; char my_a = 'a'; char my_z = 'z'; printf("n. ASCII value for A is %d", my_A); printf("n. ASCII value for Z is %d", my_Z); printf("n. ASCII value for a is %d", my_a); printf("n. ASCII value for z is %d", my_z); printf("n"); printf("n 65 in ASCII represents %c", 65); printf("n 90 in ASCII represents %c", 90); printf("n 97 in ASCII represents %c", 97); printf("n 122 in ASCII represents %c", 122); return 0; } 7 Output ASCII value for A is 65 ASCII value for Z is 90 ASCII value for a is 97 ASCII value for z is 122 65 in ASCII represents A 90 in ASCII represents Z 97 in ASCII represents a 122 in ASCII represents z

What is a String? � A string in C is an array of characters

What is a String? � A string in C is an array of characters ending with the null character (‘’). It is written inside a double quotation mark (“ ”) � A string with no characters is called a null or empty string. “ ” is the empty string. � A string may include letters, digits and various special characters such as +, -, *, ? and $. � Eg: “John Doe” “ 99999 Main Street” “Kangar, Perlis” “(012) 123 -8755” 8 (a name) (a street address) (a city and a state) (a telephone number)

What is a String? –cont. �Every character in a string has a relative position

What is a String? –cont. �Every character in a string has a relative position in the string. �The position of the first character is 0, position of the second is 1, and so on. �The length of a string is the number of character in it. �A string may be assigned (in a declaration) to either a char array or to a char pointer: Eg: 9 char color[10] = “green”; char *color = “green”; array pointer

Example 10 String Position of a Character in the String “William Jacob” Position of

Example 10 String Position of a Character in the String “William Jacob” Position of ‘W’ is 0 Position of the first ‘i’ is 1 Position of ‘ ’ (the space) is 7 Position of ‘J’ is 8 Position of ‘b’ is 12 13 “Mickey” Position of Position of 6 ‘M’ is 0 ‘i’ is 1 ‘c’ is 2 ‘k’ is 3 ‘e’ is 4 ‘y’ is 5 Length of the String

Example: string and ‘�’ #include <stdio. h> int main(void) /* a program that counts

Example: string and ‘’ #include <stdio. h> int main(void) /* a program that counts the number of characters in a string */ { char sentence[] = "I love Malaysia"; int i, count = 0; for (i = 0; sentence[i] != ''; i++) { count++; } printf(“%s has %d characters including the whitespace", sentence, count); return 0; } Sample output: 11 I love Malaysia has 15 characters including the whitespace

Fundamentals of Characters and Strings � A string can also be defined by specifying

Fundamentals of Characters and Strings � A string can also be defined by specifying the individual characters: �char color[ ] = {‘g’, ‘r’, ‘e’, ‘n’, ‘’}; � A string is accessed via a pointer to the first character in the string. � The value of a string is the address of its first character. � In memory, these are the characters stored: g 12 r e e n

Fundamentals of Characters and Strings � Notice that even though there are only five

Fundamentals of Characters and Strings � Notice that even though there are only five characters in the word ‘green’, six characters are stored in the computer. � The last character, the character ‘’, is the NULL character which indicates the end of the string. � Therefore, if an array of characters is to be used to store a string, the array must be large enough to store the string and its terminating NULL character. 13

Declaration of Strings � An example of declaration of an array (or string of

Declaration of Strings � An example of declaration of an array (or string of characters): �char info[10] = “Welcome”; or �char info[10] = “Good Bye”; char info [10]; can store a string up to 10 characters long, and may visualize it as below info � It is not necessary that this max size of 10 characters should at all the times be fully used. � info could store at any part of the program either the string of characters “Welcome” or the string “Good Bye”. 14

Declaration of Strings –cont. � A null character, constant 0 or ‘�’ can be

Declaration of Strings –cont. � A null character, constant 0 or ‘’ can be written at the end of the valid content of a string if the characters to be stored is shorter than its total length (10 in this case). � info is an array of 10 elements of type char, could be represented by storing the strings of characters “Welcome” and “Good Bye” in the following way: 15

Declaration of Strings –cont. info W e l c o m e � to

Declaration of Strings –cont. info W e l c o m e to indicate end of string G 16 o o d B y e indefinite values

Declaration of Strings –cont. • If we happen to declare a string like this:

Declaration of Strings –cont. • If we happen to declare a string like this: char my_drink[3] = “tea”; • We will get the following syntax error: initializer-string for array of chars is too long • Instead, we need to at least declare the array with (the size of the string + 1) to accommodate the null terminating character ‘’. char my_drink[4] = “tea”; 17

Initialization of Strings �Similar to array, but each character is enclosed in ‘ ’

Initialization of Strings �Similar to array, but each character is enclosed in ‘ ’ or “ ”. �Example: �char newstring[]={‘W’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’, ‘’}; �char newstring[]= “Welcome”; � ‘’ is automatically inserted. �The difference is that single quotes (‘) are used to specify single character constants and null character must be added at the end of the sentence. 18

Initialization of Strings –cont. � On the other hand, double quotes (“) are constant

Initialization of Strings –cont. � On the other hand, double quotes (“) are constant that specify sequence of characters and always have a null character (‘’) automatically inserted at the end. char newstring[]= {‘W’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’, ‘’}; char newstring[] = “Welcome”; Single quotes – null char must be added Double quotes – null char automatically inserted 19

Initialization of Strings –cont. � The examples below are not valid for string of

Initialization of Strings –cont. � The examples below are not valid for string of characters (array). newstring = “Welcome”; //no [] and data type newstring [] =“Welcome”; //no data type newstring = {‘W’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’, ‘’}; //no [] and data type 20

Assigning Values to String � The left hand side value of an assignation can

Assigning Values to String � The left hand side value of an assignation can only be array items and not the entire array, a possible way to assign a string of characters to an array of char can be shown as: newstring[0] = ‘W’; newstring[1] = ‘e’; newstring[2] = ‘l’; newstring[3] = ‘c’; newstring[4] = ‘o’; newstring[5] = ‘m’; newstring[6] = ‘e’; newstring[7] = ‘’; 21

Calculation of String Size � char is 1 byte, the total number of alphabets

Calculation of String Size � char is 1 byte, the total number of alphabets plus a null would be the size of the string. Example program: #include <stdio. h> #include <string. h> char newstring[] = {'W', 'e', 'l', 'c', 'o', 'm', 'e', ''}; char mystring[] = "Good Bye"; int main() { printf ("Size of newstring is %d", sizeof (newstring)); //size of string Welcome printf ("n. Size of mystring is %d", sizeof (mystring)); // size of string Good Bye return 0; } What is the output? Size of newstring is 8 Size of mystring is 9 22

Exercise #include<stdio. h> #include<string. h> int main() { char string 1[10]={"Welcome"}; char string 2[]={'W',

Exercise #include<stdio. h> #include<string. h> int main() { char string 1[10]={"Welcome"}; char string 2[]={'W', 'e', 'l', 'c', 'o', 'm', 'e', ''}; char string 3 []="Good Bye"; Display content of string 1: Welcome Display content of string 2: Welcome Display content of string 3: Good Bye Size of string 1 is 10 Size of string 2 is 8 Size of string 3 is 9 Length of string 1 is 7 Length of string 2 is 7 Length of string 3 is 8 printf("Display content of string 1: %sn", string 1); printf("Display content of string 2: %sn", string 2); printf("Display content of string 3: %sn", string 3); printf("nn. Size of string 1 is %d", sizeof (string 1)); printf("n. Size of string 2 is %d", sizeof (string 2)); printf("n. Size of string 3 is %d", sizeof (string 3)); printf("nn. Length of string 1 is %d", strlen (string 1)); printf("n. Length of string 2 is %d", strlen (string 2)); printf("n. Length of string 3 is %d", strlen (string 3)); return 0; 23 }

Character and String Manipulation �Examples: �a program may need to verify that an ID

Character and String Manipulation �Examples: �a program may need to verify that an ID number of first year students starts with ‘ 09’. �determine whether last three characters in a part number are valid. �Built-in functions available – makes it easier. �Before using built-in functions in strings, use string header file, #include<string. h> 24

Character and String Manipulation –cont. 25 Built in Functions for String Handling � strcat

Character and String Manipulation –cont. 25 Built in Functions for String Handling � strcat Appends a string � strchr Finds first occurrence of a given character � strcmp Compares two strings � strcmpi Compares two strings, non-case sensitive � strcpy Copies one string to another � strlen Finds length of a string � strlwr Converts a string to lowercase � strnca t Appends n characters of string � strncmp Compares n characters of two strings � strncpy Copies n characters of one string to another � strnset Sets n characters of string to a given character � strrchr Finds last occurrence of given character in string � strrev Reverses string � strset Sets all characters of string to a given character � strspn Finds first substring from given character set in string � strupr Converts string to uppercase

Controlling the Case of a Character Example char name[]; printf (“Enter a name :

Controlling the Case of a Character Example char name[]; printf (“Enter a name : ”); scanf (“%s”, name); strupr(name); printf("The name in uppercase is %s", name); 26

Controlling the Case of a Character Sample Program To convert a string to uppercase

Controlling the Case of a Character Sample Program To convert a string to uppercase #include <stdio. h> #include <string. h> int main() { char name[20]; /* declare an array of characters 0 -19 */ printf("Enter in a name in lowercasen"); scanf( "%s", name ); strupr(name); printf("The name in uppercase is %s", name ); return 0; } Output 27 Enter in a name in lowercase john The name in uppercase is JOHN