CS161_Topic#12

CS161_Topic#12 - CS161 CS161 Introduction to Computer...

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

View Full Document Right Arrow Icon
1 CS161 CS161 Introduction to Introduction to Computer Science Computer Science Topic #12
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS161 Topic #12 2 Today in CS161 More about Strings Comparing and copying strings Accessing single characters in a string Write a program with strings
Background image of page 2
CS161 Topic #12 3 Operations on Strings • There are very few operations that can be  performed on array of characters (i.e., strings) • We can read in string using: cin >> array_of_characters; cin.get(array, size, delimiter); • We display strings using: cout << array_of_characters; • But, there are no others. ..
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS161 Topic #12 4 Operations on Strings • For example, we cannot  compare two strings by  saying: char str1[10], str2[10]; if (str1 == str2) This is because an array is really the address of the first element in a sequentially allocated set of memory . So, the == or != operators would simply be comparing the memory addresses! Oops!
Background image of page 4
CS161 Topic #12 5 Comparing Strings: • Instead, to compare two strings we must include  another library:  string.h • And, call the string compare function: strcmp(first_array, second_array); The strcmp function returns:
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

CS161_Topic#12 - CS161 CS161 Introduction to Computer...

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

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