Lecture 10 String What is a String n





![Character Array (string of characters) n char info [10]; n n n Can store Character Array (string of characters) n char info [10]; n n n Can store](https://slidetodoc.com/presentation_image_h/132c3af937b99ee84834d0ff5894f7b0/image-6.jpg)










![Controlling the Case of a Character (Example) char s 1[] = "abc"; . . Controlling the Case of a Character (Example) char s 1[] = "abc"; . .](https://slidetodoc.com/presentation_image_h/132c3af937b99ee84834d0ff5894f7b0/image-17.jpg)









- Slides: 26
Lecture 10: String
What is a String? n n n The data type string is a programmerdefined and it is not part of the C language. The C standard library supplies it. A string is an array of character. 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. Every single character can have its own ASCII value in the system.
ASCII Table
Example String “William Jacob” “Mickey” Position of a Character in the String 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 Position Position of of of ‘M’ is 0 ‘i’ is 1 ‘c’ is 2 ‘k’ is 3 ‘e’ is 4 ‘y’ is 5 Length of the String 13 6
String Type n n To use the data type string, the program must include the header file string. #include <string. h> 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 variable info is capable of storing (just about) any size string.
Character Array (string of characters) n char info [10]; n n n Can store “Welcome”, “Good Bye”. char info [] = {‘W’, ’e’, ’l’, ’c’, ’o’, ’m’, ’e’, ’ ’}; char name [50]; Able to store strings that is shorter than the total length. The last value in the string will be null character (‘ ’).
Declaration of Strings n An example of declaration of an array (or string of characters): char info [10]; can store a string up to 10 characters long, and may visualize it as below info n It is no 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”.
Declaration of Strings (cont. . ) info W e l c o m e to indicate end of string G o o d B y e indefinite values
Initialization of string n n Same like other array, but each character is enclosed in ‘ ‘ or “ “. Eg: n n char newstring[]={‘W’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’, ‘ ’}; char newstring[]= “Welcome”; n n ‘ ’ 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.
Initialization of string (cont…) n On the other hand, double quotes (“) are constant that specify sequence of characters and between double quotes have always a null character (‘ ’) automatically inserted at the end. char newstring[]= {‘W’, ’e’, ’l’, ’c’, ’o’, ’m’, ’e’, ’ ’}; Single quotes – null char must be added char newstring[] = “Welcome”; Double quotes – null char automatically inserted
Initialization of string (cont…) n 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
Assigning values to string n The left hand side value of an assignation can only be array items and not the entire array, the probable way to assign a string of characters to an array of char can be shown as: char newstring[]= {‘W’, ’e’, ’l’, ’c’, ’o’, ’m’, ’e’, ’ ’}; newstring[0] = ‘W’; newstring[1] = ‘e’; newstring[2] = ‘l’; newstring[3] = ‘c’; newstring[4] = ‘o’; newstring[5] = ‘m’; newstring[6] = ‘e’; newstring[7] = ‘ ’;
Calculation of string size n 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', '