Additional_Materials_(4_on_1)

Additional_Materials_(4_on_1) - Topics Additional Materials...

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

View Full Document Right Arrow Icon
Additional Materials For Your Reference only [ CS1102C Additional Materials Topics ± C Related: ² C-Style String ± C++ Related: ² Name Space C-Style String The good old C way of doing things [ 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 1

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

View Full DocumentRight Arrow Icon
[ 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 [ 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 [ 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 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

Additional_Materials_(4_on_1) - Topics Additional Materials...

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

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