SFTW120-LectureNotes-10

SFTW120-LectureNotes-10 - SFTW120 Programming Science...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 SFTW120 Programming Science Lecture Notes 10: C: Strings (Textbook Chapter 9) © Robert P. Biuk-Aghai SFTW120 - Programming Science 2 Lecture Notes 10 Strings s A string is a data structure that contains a group of characters s Simple character data can be represented using the char data type; but this can be used for only one character s To process and display more than one character at a time, strings have to be used s Example of using a string: printf( "Average = %.2f" , avg); b "Average = %.2f" is a string constant of 14 characters (including space characters)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 SFTW120 - Programming Science 3 Lecture Notes 10 String variables s Strings in C are arrays of characters s Example of declaring a string variable: char name[10]; b This creates the array called name with 10 elements of type char s Initializing a string variable: char name[10] = "John"; s String variables can hold strings of varying length; a string variable of length n can hold a string from 0 to n -1 characters long (e.g. the string in name is 4 characters long) SFTW120 - Programming Science 4 Lecture Notes 10 String termination character s String declared by char name[10] = "John"; in memory: s At the end of the string John , there is a special character: \0 , the null character s The null character marks the end (termination) of the string; every string variable must have a null character at the end (so a length n string variable must keep 1 character for the null character, so it can use only n -1 characters for the string itself) ? ? ? ? ? \0 n h o J [9] [8] [7] [6] [5] [4] [3] [2] [1] [0]
Background image of page 2
3 SFTW120 - Programming Science 5 Lecture Notes 10 String termination character s By using a null character as the string termination character , it is possible for a program to know where the end of a varying length string is s String processing functions in C ignore anything that is stored after the null character s The longest possible string that can be stored in char name[10] has 9 characters; e.g.: \0 r e d n a x e l A [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] SFTW120 - Programming Science 6 Lecture Notes 10 Arrays of strings s When several strings need to be stored together, an array of strings can be declared; this is an array of character arrays, i.e. a two-dimensional array of char s Example: char months[12][10]; b This creates the array months which can have 12 strings of 10 characters each (including the null character) b Initializing an array of strings: char months[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 SFTW120 - Programming Science 7 Lecture Notes 10 Printing strings s The printf function can print the value of string variables using the %s string placeholder: char name[10] = "John"; printf("Hello, %s!\n", name); Output: Hello, John! s
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 32

SFTW120-LectureNotes-10 - SFTW120 Programming Science...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online