summaryrefslogtreecommitdiff
path: root/Schaum's_Outlines:_Programming_with_C++/ch8.ipynb
diff options
context:
space:
mode:
authorroot2014-07-14 15:31:28 +0530
committerroot2014-07-14 15:31:28 +0530
commit5a3a66e158c99bbf7900278d8e7056136d628dbe (patch)
tree6d8c0f65acb3e9451d5092e8d718e7febcbd0d05 /Schaum's_Outlines:_Programming_with_C++/ch8.ipynb
parent69aa63da6cb2bc3f5822533e3f97c46d7efbaafc (diff)
downloadPython-Textbook-Companions-5a3a66e158c99bbf7900278d8e7056136d628dbe.tar.gz
Python-Textbook-Companions-5a3a66e158c99bbf7900278d8e7056136d628dbe.tar.bz2
Python-Textbook-Companions-5a3a66e158c99bbf7900278d8e7056136d628dbe.zip
adding books
Diffstat (limited to 'Schaum's_Outlines:_Programming_with_C++/ch8.ipynb')
-rwxr-xr-xSchaum's_Outlines:_Programming_with_C++/ch8.ipynb1064
1 files changed, 1064 insertions, 0 deletions
diff --git a/Schaum's_Outlines:_Programming_with_C++/ch8.ipynb b/Schaum's_Outlines:_Programming_with_C++/ch8.ipynb
new file mode 100755
index 00000000..2b83cace
--- /dev/null
+++ b/Schaum's_Outlines:_Programming_with_C++/ch8.ipynb
@@ -0,0 +1,1064 @@
+{
+ "metadata": {
+ "name": "ch8"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 8: C- Strings"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.1, page no. 184"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "n= [44] # n holds the int 44\n",
+ "print \"int n=44; // n holds the int 44:\\n\";\n",
+ "print \"\\t\\t n = \" , n \n",
+ "print \"\\t\\t &n = \" , hex(id(n))\n",
+ "pn = n \n",
+ "print \"int* pn=&n; // pn holds the address of n:\\n\";\n",
+ "print \"\\t\\t n = \" , n \n",
+ "print \"\\t\\t &n = \" , hex(id(n))\n",
+ "print \"\\t\\t pn = \" , hex(id(pn)) \n",
+ "print \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"\\t\\t *pn = \" , pn\n",
+ "\n",
+ "pn[0] = 77 # changes the value of n to 77\n",
+ "print \"*pn = 77; // changes the value of n to 77:\\n\";\n",
+ "print \"\\t\\t n = \" , n \n",
+ "print \"\\t\\t &n = \" , hex(id(n))\n",
+ "print \"\\t\\t pn = \" , hex(id(pn)) \n",
+ "print \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"\\t\\t *pn = \" , pn\n",
+ "\n",
+ "q = n \n",
+ "print \"int* q=&n; // q also holds the address of n:\\n\";\n",
+ "print \"\\t\\t n = \" , n \n",
+ "print \"\\t\\t &n = \" , hex(id(n))\n",
+ "print \"\\t\\t pn = \" , hex(id(pn)) \n",
+ "print \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\n",
+ "print \"\\t\\t *pn = \" , pn\n",
+ "print \"\\t\\t q = \" , hex(id(q))\n",
+ "print \"\\t\\t &q = \" , hex(id(hex(id(hex(id(pn))))))\n",
+ "print \"\\t\\t *q = \" , q \n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "int n=44; // n holds the int 44:\n",
+ "\n",
+ "\t\t n = [44]\n",
+ "\t\t &n = 0x9bfb92c\n",
+ "int* pn=&n; // pn holds the address of n:\n",
+ "\n",
+ "\t\t n = [44]\n",
+ "\t\t &n = 0x9bfb92c\n",
+ "\t\t pn = 0x9bfb92c\n",
+ "\t\t &pn = 0x9bf5aa0\n",
+ "\t\t *pn = [44]\n",
+ "*pn = 77; // changes the value of n to 77:\n",
+ "\n",
+ "\t\t n = [77]\n",
+ "\t\t &n = 0x9bfb92c\n",
+ "\t\t pn = 0x9bfb92c\n",
+ "\t\t &pn = 0x9c6a760\n",
+ "\t\t *pn = [77]\n",
+ "int* q=&n; // q also holds the address of n:\n",
+ "\n",
+ "\t\t n = [77]\n",
+ "\t\t &n = 0x9bfb92c\n",
+ "\t\t pn = 0x9bfb92c\n",
+ "\t\t &pn = 0x9bf5c80\n",
+ "\t\t *pn = [77]\n",
+ "\t\t q = 0x9bfb92c\n",
+ "\t\t &q = 0x9c6a760\n",
+ "\t\t *q = [77]\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.2, page no. 185"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s = \"ABCD\"\n",
+ "for i in range(4):\n",
+ " print \"s[\" , i , \"] = '\" , s[i] , \"'\\n\";\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "s[ 0 ] = ' A '\n",
+ "\n",
+ "s[ 1 ] = ' B '\n",
+ "\n",
+ "s[ 2 ] = ' C '\n",
+ "\n",
+ "s[ 3 ] = ' D '\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.3, page no. 186"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "while True:\n",
+ " word = raw_input()\n",
+ " if len(word) < 2:\n",
+ " break\n",
+ " l = word.split(' ')\n",
+ " for i in l:\n",
+ " print '\\t\"' , i , '\"'\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Today's date is March 12, 2000.\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t\" Today's \"\n",
+ "\t\" date \"\n",
+ "\t\" is \"\n",
+ "\t\" March \"\n",
+ "\t\" 12, \"\n",
+ "\t\" 2000. \"\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Tomorrow is Monday.\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t\" Tomorrow \"\n",
+ "\t\" is \"\n",
+ "\t\" Monday. \"\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.4, page no. 187"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "while True:\n",
+ " line = raw_input()\n",
+ " if len(line) < 2:\n",
+ " break\n",
+ " print \"\\t[\" , line , \"]\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Once upon a midnight dreary, while I pondered, weak and weary,\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t[ Once upon a midnight dreary, while I pondered, weak and weary, ]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Over a many quaint and curious volume of forgotten lore,\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t[ Over a many quaint and curious volume of forgotten lore, ]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.5, page no. 187"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "while True:\n",
+ " word = raw_input()\n",
+ " if len(word) < 2:\n",
+ " break\n",
+ " l = word.split(',')\n",
+ " for i in range(len(l)-1):\n",
+ " print '\\t[' , l[i] , ']'\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Once upon a midnight dreary, while I pondered, weak and weary,\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t[ Once upon a midnight dreary ]\n",
+ "\t[ while I pondered ]\n",
+ "\t[ weak and weary ]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Over a many quaint and curious volume of forgotten lore,\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t[ Over a many quaint and curious volume of forgotten lore ]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.6, page no. 188"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "count = 0\n",
+ "while True:\n",
+ " a = raw_input()\n",
+ " if len(a) < 1:\n",
+ " break\n",
+ " for ch in a:\n",
+ " if (ch == 'e'): count+=1\n",
+ " \n",
+ "print count , \" e's were counted.\\n\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Once upon a midnight dreary, while I pondered, weak and weary,\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Over many a quaint and curious volume of forgotten lore,\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "11 e's were counted.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.7, page no. 188"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "while True:\n",
+ " a = raw_input()\n",
+ " if len(a) < 1:\n",
+ " break\n",
+ " print a.title()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Fourscore and seven years ago our fathers\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Fourscore And Seven Years Ago Our Fathers\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "brought forth upon this continent a new nation,\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Brought Forth Upon This Continent A New Nation,\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.8, page no. 189"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "a = raw_input()\n",
+ "l = a.split(' ')\n",
+ "nos = []\n",
+ "for i in l:\n",
+ " try:\n",
+ " i = int(i)\n",
+ " nos.append(i)\n",
+ " except:\n",
+ " continue\n",
+ "m = nos[0]\n",
+ "n = nos[1] \n",
+ "print m , \" + \" , n , \" = \" , m+n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "what is 305 plus 9416 ?\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "305 + 9416 = 9721\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.10, page no. 191"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "count=0\n",
+ "\n",
+ "print \"Enter at most 4 names with at most 19 characters:\\n\";\n",
+ "while (True):\n",
+ " n = raw_input()\n",
+ " if len(n) < 1:\n",
+ " break\n",
+ " name.append(n)\n",
+ " count += 1\n",
+ " \n",
+ "print \"The names are:\\n\"\n",
+ "for i in range(count):\n",
+ " print \"\\t\" , i , \". [\" , name[i] , \"]\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter at most 4 names with at most 19 characters:\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "George Washington\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "John Adams\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Thomas Jefferson\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The names are:\n",
+ "\n",
+ "\t0 . [ George Washington ]\n",
+ "\t1 . [ John Adams ]\n",
+ "\t2 . [ Thomas Jefferson ]\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.11, page no. 192"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "name = []\n",
+ "count=0\n",
+ "\n",
+ "print \"Enter at most 4 names with at most 19 characters:\\n\";\n",
+ "while (True):\n",
+ " n = raw_input()\n",
+ " if len(n) < 1:\n",
+ " break\n",
+ " name.append(n)\n",
+ " count += 1\n",
+ " \n",
+ "print \"The names are:\\n\"\n",
+ "for i in range(count):\n",
+ " print \"\\t\" , i , \". [\" , name[i] , \"]\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter at most 4 names with at most 19 characters:\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "George Washington\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "John Adams\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Thomas Jefferson\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The names are:\n",
+ "\n",
+ "\t0 . [ George Washington ]\n",
+ "\t1 . [ John Adams ]\n",
+ "\t2 . [ Thomas Jefferson ]\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.12, page no. 193"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "name = [ \"George Washington\", \"John Adams\", \"Thomas Jefferson\"]\n",
+ "print \"The names are:\\n\"\n",
+ "for i in range(3):\n",
+ " print \"\\t\" , i , \". [\" , name[i] , \"]\"\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The names are:\n",
+ "\n",
+ "\t0 . [ George Washington ]\n",
+ "\t1 . [ John Adams ]\n",
+ "\t2 . [ Thomas Jefferson ]\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.13, page no. 193"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s = \"ABCDEFG\"\n",
+ "print \"len(\" , s , \") = \" , len(s) \n",
+ "print \"len(\\\"\\\") = \" , len(\"\")\n",
+ "print \"Enter string: \"\n",
+ "b = raw_input()\n",
+ "print \"len(\" , b , \") = \" , len(b)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "len( ABCDEFG ) = 7\n",
+ "len(\"\") = 0\n",
+ "Enter string: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "hello how are you !!!\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "len( hello how are you !!! ) = 21\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.14, page no. 194"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s = \"The Mississippi is a long river.\"\n",
+ "print 's = \"' , s , '\"'\n",
+ "p = s.find(' ')\n",
+ "print \"find(s, ' ') points to s[\" , p , \"].\"\n",
+ "p = s.find('s')\n",
+ "print \"find(s, 's') points to s[\" , p , \"].\"\n",
+ "p = s.rfind('s')\n",
+ "print \"reverse find(s, 's') points to s[\" , p , \"].\"\n",
+ "p = s.find(\"is\")\n",
+ "print \"strstr(s, \\\"is\\\") points to s[\" , p , \"].\"\n",
+ "p = s.find(\"isi\")\n",
+ "if p== -1:\n",
+ " print 's.find(\"isi\") returns NULL'\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "s = \" The Mississippi is a long river. \"\n",
+ "find(s, ' ') points to s[ 3 ].\n",
+ "find(s, 's') points to s[ 6 ].\n",
+ "reverse find(s, 's') points to s[ 17 ].\n",
+ "strstr(s, \"is\") points to s[ 5 ].\n",
+ "s.find(\"isi\") returns NULL\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.15, page no. 195"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s1 = \"ABCDEFG\"\n",
+ "s2 = \"XYZ\" \n",
+ "print \"Before strcpy(s1,s2):\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n",
+ "s1 = s2\n",
+ "print \"After strcpy(s1,s2):\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Before strcpy(s1,s2):\n",
+ "\n",
+ "\ts1 = [ ABCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n",
+ "After strcpy(s1,s2):\n",
+ "\n",
+ "\ts1 = [ XYZ ], length = 3\n",
+ "\ts2 = [ XYZ ], length = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.16, page no. 195"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s1 = \"ABCDEFG\"\n",
+ "s2 = \"XYZ\" \n",
+ "print \"Before strcpy(s1,s2,2):\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n",
+ "s1 = s2[:2] + s1[2:]\n",
+ "print \"After strcpy(s1,s2,2):\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Before strcpy(s1,s2,2):\n",
+ "\n",
+ "\ts1 = [ ABCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n",
+ "After strcpy(s1,s2,2):\n",
+ "\n",
+ "\ts1 = [ XYCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.17, page no. 196"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s1 = \"ABCDEFG\"\n",
+ "s2 = \"XYZ\" \n",
+ "print \"Before string concatination :\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n",
+ "s1 += s2\n",
+ "print \"After string concatination :\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Before string concatination :\n",
+ "\n",
+ "\ts1 = [ ABCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n",
+ "After string concatination :\n",
+ "\ts1 = [ ABCDEFGXYZ ], length = 10\n",
+ "\ts2 = [ XYZ ], length = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.18, page no. 197"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s1 = \"ABCDEFG\"\n",
+ "s2 = \"XYZ\" \n",
+ "print \"Before string concatination :\\n\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n",
+ "s1 += s2[:2]\n",
+ "print \"After string concatination :\" \n",
+ "print \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \n",
+ "print \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Before string concatination :\n",
+ "\n",
+ "\ts1 = [ ABCDEFG ], length = 7\n",
+ "\ts2 = [ XYZ ], length = 3\n",
+ "After string concatination :\n",
+ "\ts1 = [ ABCDEFGXY ], length = 9\n",
+ "\ts2 = [ XYZ ], length = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.19, page no. 197"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s = \"Today's date is March 12, 2000.\"\n",
+ "\n",
+ "print \"The string is: [\" , s , \"] \\nIts tokens are: \"\n",
+ "p = s.split(\" \")\n",
+ "\n",
+ "for i in p:\n",
+ " print \"\\t[\" , i , \"] \"\n",
+ "\n",
+ "print \"Now the string is: [\" , p[0] , \"] \";\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The string is: [ Today's date is March 12, 2000. ] \n",
+ "Its tokens are: \n",
+ "\t[ Today's ] \n",
+ "\t[ date ] \n",
+ "\t[ is ] \n",
+ "\t[ March ] \n",
+ "\t[ 12, ] \n",
+ "\t[ 2000. ] \n",
+ "Now the string is: [ Today's ] \n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8.20, page no. 199"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def strpbrk(s,s1):\n",
+ " found = []\n",
+ " for i in range(len(s1)):\n",
+ " if s1[i] in s:\n",
+ " index = s.find(s1[i])\n",
+ " found.append(index)\n",
+ " if found:\n",
+ " return min(found)\n",
+ " return None\n",
+ " \n",
+ "\n",
+ "s = \"The Mississippi is a long river.\"\n",
+ "print 's = \"' , s , '\"'\n",
+ "p = strpbrk(s, \"nopqr\")\n",
+ "print 'strpbrk(s, \"nopqr\") points to s[' , p , \"].\"\n",
+ "p = strpbrk(s, \"NOPQR\")\n",
+ "if (p == None):\n",
+ " print 'strpbrk(s, \"NOPQR\") returns NULL.\\n'"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "s = \" The Mississippi is a long river. \"\n",
+ "strpbrk(s, \"nopqr\") points to s[ 12 ].\n",
+ "strpbrk(s, \"NOPQR\") returns NULL.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 22
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file