Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character.

Similar presentations


Presentation on theme: "Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character."— Presentation transcript:

1 Week 7 – String

2 Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character Array Declaration of Strings Fundamentals of Strings & Characters Initialization of Strings Assigning Values to Strings Calculation of String Size String Conversion Functions Comparison Functions of the Strings ASCII Table

3 Introduction : What is a String? The data type string is a programmer-defined and it not part of the C language. The C standard library supplies it. The data type string is a programmer-defined and it not part of the C language. The C standard library supplies it. A string with no characters is called a null or empty string. A string with no characters is called a null or empty string. “ ” is the empty string. “ ” is the empty string. Every character in a string has a relative position in the string. 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 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. The length of a string is the number of character in it. Strings can be treated as array of type char used to store names of people, places, or anything that involves a combination of letters. Strings can be treated as array of type char used to store names of people, places, or anything that involves a combination of letters. However, as number can be stored as characters, a string can be an array of numbers, too. However, as number can be stored as characters, a string can be an array of numbers, too.

4 KUKUM Sem1-06/07 4EKT120: Computer Programming String Type To use the data type string, the program must include the header file string. To use the data type string, the program must include the header file string. #include #include The statement The statement string info = “Welcome”; declares info to be string variable and also initializes info to “Welcome”. The position of the first character, ‘W’, in the info is 0, the position of the second character, ‘e’, is 1, and so on. The position of the first character, ‘W’, in the info is 0, the position of the second character, ‘e’, is 1, and so on. The variable info is capable of storing (just about) any size string. The variable info is capable of storing (just about) any size string.

5 KUKUM Sem1-06/07 5EKT120: Computer Programming Character Array (string of characters) char info [10]; char info [10]; Can store “Welcome”, “Good Bye”. Can store “Welcome”, “Good Bye”. char name [50]; char name [50]; Able to store shorter strings that total length. Able to store shorter strings that total length. The last value in the string will be null character (‘\0’). The last value in the string will be null character (‘\0’).

6 KUKUM Sem1-06/07 6EKT120: Computer Programming Declaration of Strings An example of declaration of an array (or string of characters): An example of declaration of an array (or string of characters): 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”. 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”. info char info [10]; can store a string up to 10 characters long, and may visualize it as below

7 KUKUM Sem1-06/07 7EKT120: Computer Programming Declaration of Strings (cont..) A null character, constant 0 or ‘\0’ can be written at the end of the valid content of a string if the characters to be stored is shorter strings that its total length (10 in this case). A null character, constant 0 or ‘\0’ can be written at the end of the valid content of a string if the characters to be stored is shorter strings that 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…………. 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………….

8 KUKUM Sem1-06/07 8EKT120: Computer Programming Declaration of Strings (cont..) W info elcome\0 GeyBdoO to indicate end of string indefinite values

9 KUKUM Sem1-06/07 9EKT120: Computer Programming Fundamentals of Strings and Characters String declarations String declarations Declare as a character array or a variable of type char * Declare as a character array or a variable of type char * char info[] = “Welcome"; char *infoPtr = “Welcome"; Remember that strings represented as character arrays end with '\0' Remember that strings represented as character arrays end with '\0' info has 8 elements info has 8 elements Inputting strings Inputting strings Use scanf Use scanf scanf(“ %s ", word); Copies input into word[] Copies input into word[] Do not need & (because a string is a pointer) Do not need & (because a string is a pointer) Remember to leave room in the array for '\0' Remember to leave room in the array for '\0'

10 KUKUM Sem1-06/07 10EKT120: Computer Programming Initialization of string Same like other array, but each character is enclosed in ‘ ’ or “ ”. Same like other array, but each character is enclosed in ‘ ’ or “ ”. 2 ways of initializing the string as follow…. 2 ways of initializing the string as follow…. char newstring[]={‘W’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’, ‘\0’}; char newstring[]={‘W’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’, ‘\0’}; char newstring[]= “Welcome”; char newstring[]= “Welcome”; ‘\0’ is automatically inserted. ‘\0’ 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. 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.

11 KUKUM Sem1-06/07 11EKT120: Computer Programming Initialization of string (cont…) On the other hand, double quotes (“) are constant that specify sequence of characters and between double quotes have always a null character (‘\0’) automatically inserted at the end. On the other hand, double quotes (“) are constant that specify sequence of characters and between double quotes have always a null character (‘\0’) automatically inserted at the end. char newstring[]= {‘W’,’e’,’l’,’c’,’o’,’m’,’e’,’\0’}; char newstring[] = “Welcome”; Single quotes – null char must be added Double quotes – null char automatically inserted

12 KUKUM Sem1-06/07 12EKT120: Computer Programming The examples below are not valid for string of characters (array). 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’,’\0’}; // no [ ] and data type Initialization of string (cont…)

13 KUKUM Sem1-06/07 13EKT120: Computer Programming Determining the size of the string sizeof - function to get the size of the variable in term of bytes. sizeof - function to get the size of the variable in term of bytes. - Special unary operator used to determine the size in bytes of an array (or any other data type) during program compilation. Example float array[20] What is the size of the array??? What is the size of the array in bytes????

14 KUKUM Sem1-06/07 14EKT120: Computer Programming Size = 20 Elements Size in bytes ????????? Variables of type float are normally stored in 4 bytes of memory, and array is defined to have 20 elements. Therefore the size in bytes……. 20 x 4 bytes = 80 bytes Note for following variables char are normally stored in 1 byte of memory char are normally stored in 1 byte of memory double are normally stored in 8 bytes of memory double are normally stored in 8 bytes of memory Etc Etc

15 KUKUM Sem1-06/07 15EKT120: Computer Programming Programming example char newstring[] = {'W','e','l','c','o','m','e','\0'}; char mystring[] = "Good Bye"; printf ("Size of newstring is %d", sizeof (newstring)); printf ("\nSize of mystring is %d", sizeof (mystring)); Why this this program is written?? What is the output of this program???

16 KUKUM Sem1-06/07 16EKT120: Computer Programming Output Size of newstring is 8 Size of mystring is 9 Explanation Welcome  char = 1 byte number of alphabets = 7 x 1 byte Good Bye  char = 1 byte number of alphabets = 8 x 1 byte

17 StringPosition of a Character Length of the String in the String “William Jacob”Position of ‘W’ is 013 Position of the first ‘i’ is 1 Position of ‘ ’ (the space) is 7 Position of ‘J’ is 8 Position of ‘b’ is 12 “Mickey”Position of ‘M’ is 06 Position of ‘i’ is 1 Position of ‘c’ is 2 Position of ‘k’ is 3 Position of ‘e’ is 4 Position of ‘y’ is 5 Determining length of the string

18 KUKUM Sem1-06/07 18EKT120: Computer Programming strlen - A function to get the length of the string. Programming example char newstring[] = {'W','e','l','c','o','m','e','\0'}; char mystring[] = "Good Bye"; printf ("Size of newstring is %d", sizeof (newstring)); printf ("\nSize of mystring is %d", sizeof (mystring)); printf ("\n\nLength of newstring is %d",strlen(newstring)); printf ("\nLength of mystring is %d", strlen(mystring)); Again…………… Why this this program is written?? What is the output of this program???


Download ppt "Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character."

Similar presentations


Ads by Google