Additional_Materials_(1_on_1)

Additional_Materials_(1_on_1) - Additional Materials For...

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

View Full Document Right Arrow Icon
Additional Materials For Your Reference only
Background image of page 1

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

View Full DocumentRight Arrow Icon
[ CS1102C Additional Materials Topics ± C Related: ² C-Style String ± C++ Related: ² Name Space
Background image of page 2
C-Style String The good old C way of doing things
Background image of page 3

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

View Full DocumentRight Arrow Icon
[ CS1102C Additional Materials C-Style String ± String (a list of characters) is a frequently used data type ± C++ provides a new implementation of string ² Known as the string class ² Will be covered in the next lecture ± C-style string is still useful ² An array of characters ² Has a ‘\0 ’ (null character) at the end as string terminator ² Includes C-Style String Library #include <cstring> ² Useful functions : strcpy, strcmp, strcat etc.
Background image of page 4
[ CS1102C Additional Materials Example : Common cstring functions #include <iostream> #include < cstring > using namespace std; int main () { char s1[20] = “abc”, s2[20]; strcpy (s2, "xyz"); cout << "S1 = " << s1 << endl; cout << "S2 = " << s2 << endl; cout << "S1 + S2 = " << strcat(s1, s2) << endl; cout << "S2 + S1 = " << strcat(s2, s1) << endl; if ( strcmp (s1, s2) > 0) cout << "S1 > S2" << endl; else cout << "S1 <= S2" << endl; } Output: S1 = abc S2 = xyz S1 + S2 = abcxyz S2 + S1 = xyzabcxyz S1 <= S2
Background image of page 5

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

View Full DocumentRight Arrow Icon
[ CS1102C Additional Materials cstring : Input and Output ± Output using the insertion operator (output operator) << ± Input using the extraction operator (input operator) >> ² Reading stops whenever space is encountered, i.e. only a word is read char a [80], b[80]; cout << “Enter something:\n”; cin >> a >> b; cout << a << b << “END\n”; Sample Execution: Enter something: What A Wonderful World!! WhatAEND
Background image of page 6
[ CS1102C Additional Materials cstring : Input and Output ± To read a whole sentence instead of single word ² Use the getline( ) function ² It is “attached” to the cin, i.e. cin.getline(…); ± This strange syntax will be explained in subsequent lecture ² There are two parameters:
Background image of page 7

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

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

This note was uploaded on 12/02/2011 for the course CS 3211 taught by Professor Dunnowho during the Spring '11 term at National University of Singapore.

Page1 / 21

Additional_Materials_(1_on_1) - Additional Materials For...

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

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