CS31-7 - CS 31: Introduction to Computer Science I Topic 7...

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 7: C Strings 1/2010 John A. Rohr All Rights Reserved JAR 7-1 CS 31 : Introduction to Computer Science I Topic 7 C Strings
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 7: C Strings 1/2010 John A. Rohr All Rights Reserved JAR 7-2 C Strings C Strings with C++ Strings The C Language is a proper subset of C++ All of C is included in C++ This includes the string capability in C The C++ string class also provides strings Both capabilities exist simultaneously in C++ The C++ string class supports C strings
Background image of page 2
CS 31: Introduction To CS I Topic 7: C Strings 1/2010 John A. Rohr All Rights Reserved JAR 7-3 C Strings Structure C Strings are stored in a character array A terminal character must follow all the actual characters of the string The terminator is the null character The null terminator is written '\0' One extra element is required in the array
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 7: C Strings 1/2010 John A. Rohr All Rights Reserved JAR 7-4 C Strings C String Array A C String of n characters requires a character array of n+1 characters The first n characters of the array hold the actual characters of the string The last character of the array holds the null terminator There is no length identification
Background image of page 4
CS 31: Introduction To CS I Topic 7: C Strings 1/2010 John A. Rohr All Rights Reserved JAR 7-5 C Strings C String Examples char st1[10] = ""; char st1[10] = "+"; char st3[10] = "A String"; + 0 \0 1 ? 2 ? 3 ? 4 ? 5 ? 6 ? 7 ? 8 ? 9 \0 0 ? 1 ? 2 ? 3 ? 4 ? 5 ? 6 ? 7 ? 8 ? 9 A 0 sp 1 S 2 t 3 r 4 i 5 n 6 g 7 \0 8 ? 9
Background image of page 5

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 7: C Strings 1/2010 John A. Rohr All Rights Reserved JAR 7-6 C Strings C String Examples char st1[10] = "All array"; char st1[10] = "Full array"; This is not a C String! There is no null terminator at the end F 0 u 1 l 2 l 3 sp 4 a 5 r 6 r 7 a 8 y 9 A 0 l 1 l 2 sp 3 a 4 r 5 r 6 a 7 y 8 \0 9
Background image of page 6
CS 31: Introduction To CS I Topic 7: C Strings 1/2010 John A. Rohr All Rights Reserved JAR 7-7 C Strings String Literals String literals or string constants are any set of valid characters enclosed by " and " String literals are always C Strings
Background image of page 7

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

View Full DocumentRight Arrow Icon
CS 31: Introduction To CS I Topic 7: C Strings 1/2010 John A. Rohr All Rights Reserved JAR 7-8 C Strings String Literal Example char st[] = "Literal"; This is a C String! It has the required number of elements It has a null terminator at the end L 0 i 1 t 2 e 3 r 4 a 5 l 6 \0 7
Background image of page 8
CS 31: Introduction To CS I Topic 7: C Strings 1/2010 John A. Rohr All Rights Reserved JAR 7-9 C Strings Converting to a C++ String The C++ string operator = will accept a C string on the right side No libraries other than the C++ string library are required
Background image of page 9

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

View Full DocumentRight Arrow Icon
Introduction To CS I Topic 7: C Strings 1/2010 John A. Rohr All Rights Reserved JAR 7-10 C Strings Converting to a C++ String char st1[10] = "String"; string st2; st2 = st1; st2 is the C++ string "String" S 0 t 1 r 2 i 3 n 4 g 5 \0 6 ? 7
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/08/2010 for the course COM SCI 31 taught by Professor Kinsey during the Spring '98 term at UCLA.

Page1 / 49

CS31-7 - CS 31: Introduction to Computer Science I Topic 7...

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

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