diff options
Diffstat (limited to 'Let_us_C/chapter-9.ipynb')
-rw-r--r-- | Let_us_C/chapter-9.ipynb | 1197 |
1 files changed, 599 insertions, 598 deletions
diff --git a/Let_us_C/chapter-9.ipynb b/Let_us_C/chapter-9.ipynb index e74e2dd8..a712e2f0 100644 --- a/Let_us_C/chapter-9.ipynb +++ b/Let_us_C/chapter-9.ipynb @@ -1,599 +1,600 @@ -{
- "metadata": {
- "name": "chapter-9.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h1>Chapter 9: Puppetting On Strings <h1>"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Print String , Page number: 329<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#Variable declaration\n",
- "name= \"Klinsman\" # character array or string\n",
- "i = 0\n",
- "\n",
- "#while loop for printing\n",
- "while i <= 7 :\n",
- " print name[i] \n",
- " i = i + 1\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "K\n",
- "l\n",
- "i\n",
- "n\n",
- "s\n",
- "m\n",
- "a\n",
- "n\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Print String, Page number: 330<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#Variable declaration\n",
- "name= \"Klinsman\\0\" # character array or string\n",
- "i = 0\n",
- "\n",
- "#while loop for printing\n",
- "while (name[i] != '\\0') :\n",
- " print name[i]\n",
- " i = i + 1\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "K\n",
- "l\n",
- "i\n",
- "n\n",
- "s\n",
- "m\n",
- "a\n",
- "n\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Print String Using Pointer, Page number: 330<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#Variable declaration\n",
- "name = \"Klinsman\\0\" # string or character array\n",
- "i = 0\n",
- "ptr = name[i] # store base address of string\n",
- "\n",
- "#Result\n",
- "while ptr != '\\0':\n",
- " print ptr\n",
- " i = i+1\n",
- " ptr = name[i]\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "K\n",
- "l\n",
- "i\n",
- "n\n",
- "s\n",
- "m\n",
- "a\n",
- "n\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>strlen Function, Page number: 336<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "#Variable declaration\n",
- "arr = \"Bamboozled\" # character array or string\n",
- "\n",
- "#string length function\n",
- "len1 = len(arr) \n",
- "len2 = len( \"Humpty Dumpty\" )\n",
- "\n",
- "#Result\n",
- "print \"string = %s length = %d\" %( arr, len1 )\n",
- "print \"string = %s length = %d\" %(\"Humpty Dumpty\", len2 )\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "string = Bamboozled length = 10\n",
- "string = Humpty Dumpty length = 13\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>xstrlen Function, Page number: 337<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#Function definition\n",
- "def xstrlen(s):\n",
- " length = 0\n",
- " while ( s[length] != '\\0' ):\n",
- " length += 1\n",
- " return length \n",
- "\n",
- "#Variable declaration\n",
- "arr = \"Bamboozled\\0\" # character array or string\n",
- "\n",
- "#Function calls\n",
- "len1 = xstrlen(arr) \n",
- "len2 = xstrlen( \"Humpty Dumpty\\0\" )\n",
- "\n",
- "#Result\n",
- "print \"string = %s length = %d\" %( arr, len1 )\n",
- "print \"string = %s length = %d\" %(\"Humpty Dumpty\\0\", len2 )\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "string = Bamboozled\u0000 length = 10\n",
- "string = Humpty Dumpty\u0000 length = 13\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>strcpy Function, Page number: 338<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "\n",
- "#Variable declaration\n",
- "source = \"Sayonara\" \n",
- "target = [] \n",
- "\n",
- "#strcpy function\n",
- "import copy\n",
- "target = copy.copy(source)\n",
- "\n",
- "#Result\n",
- "print \"source string = \", source \n",
- "print \"target string = \", target \n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "source string = Sayonara\n",
- "target string = Sayonara\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>xstrcpy Function, Page number: 339<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#Function definition\n",
- "def xstrcpy (t,s):\n",
- " i = 0\n",
- " while ( s[i] != '\\0' ):\n",
- " t = t + s[i]\n",
- " i = i + 1\n",
- " return t\n",
- " \n",
- "#Variable declaration\n",
- "source = \"Sayonara\\0\" \n",
- "target = ''\n",
- "\n",
- "target = xstrcpy ( target, source ) # function call\n",
- "\n",
- "#Result\n",
- "print \"source string = \", source \n",
- "print \"target string = \", target "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "source string = Sayonara\u0000\n",
- "target string = Sayonara\n"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>strcat Function, Page number: 342<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#Variable declaration\n",
- "source = \"Folks!\" \n",
- "target = \"Hello\"\n",
- " \n",
- "target = target + source # string concatenation\n",
- "#Result\n",
- "print \"source string = \", source \n",
- "print \"target string = \", target \n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "source string = Folks!\n",
- "target string = HelloFolks!\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>strcmp Function, Page number: 343<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#Variable declaration\n",
- "string1 = \"Jerry\\0\" \n",
- "string2 = \"Ferry\\0\"\n",
- "\n",
- "#Function definition\n",
- "def strcmp (string1 , string2):\n",
- " if (string1 == string2):\n",
- " v = 0 #If the two strings are identical, strcmp returns a value zero\n",
- " return v\n",
- " else:\n",
- " n = 0\n",
- " while ( string1[n]):\n",
- " if ( string1[n] == string2[n]):\n",
- " n = n + 1\n",
- " continue\n",
- " else:\n",
- " v = ord(string1[n]) - ord(string2[n]) #returns the numeric difference between the ASCII values of the first non-matching pairs of characters\n",
- " return v\n",
- " return v\n",
- "\n",
- "#Function call\n",
- "i = strcmp ( string1, \"Jerry\\0\" ) \n",
- "j = strcmp ( string1, string2 ) \n",
- "k = strcmp ( string1, \"Jerry boy\\0\" )\n",
- "\n",
- "#Result\n",
- "print i,j,k"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "0 4 -32\n"
- ]
- }
- ],
- "prompt_number": 9
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Two Dimensional Array of Characters, Page number: 344<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "name against a master list to see if you are worthy of\n",
- "\n",
- "\n",
- "#Function definition\n",
- "def strcmp (string1 , string2):\n",
- " if (string1 == string2):\n",
- " v = 0 #If the two strings are identical, strcmp returns a value zero\n",
- " return v\n",
- " else:\n",
- " n = 0\n",
- " while ( string1[n]):\n",
- " if ( string1[n] == string2[n]):\n",
- " n = n + 1\n",
- " continue\n",
- " else:\n",
- " v = ord(string1[n]) - ord(string2[n]) #returns the numeric difference between the ASCII values of the first non-matching pairs of characters\n",
- " return v\n",
- " return v\n",
- "\n",
- "#Variable declaration\n",
- "FOUND = 1\n",
- "NOTFOUND = 0\n",
- "masterlist =[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n",
- "yourname = []\n",
- "flag = NOTFOUND\n",
- "\n",
- "#Input from user\n",
- "#yourname = raw_input(\"Enter your name: \")\n",
- "yourname = \"Akshatha\"\n",
- "\n",
- "#Checking in the master list\n",
- "for i in range(0,6):\n",
- " a = strcmp ( masterlist[i], yourname )\n",
- " if a == 0:\n",
- " print \"Welcome, you can enter the palace\" \n",
- " flag = FOUND\n",
- " break\n",
- " \n",
- "if flag == NOTFOUND :\n",
- " print \"Sorry, you are a trespasser\" "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Sorry, you are a trespasser\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Exchange Names using 2-D Array of Characters, Page number: 348<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#Variable declaration\n",
- "names = [\"akshay\\0\\0\\0\",\"parag\\0\\0\\0\\0\",\"raman\\0\\0\\0\\0\",\"srinivas\\0\",\"gopal\\0\\0\\0\\0\",\"rajesh\\0\\0\\0\"]\n",
- "\n",
- "#Initial condition\n",
- "print \"Original: \", names[2], names[3] \n",
- "\n",
- "#Exchanging names\n",
- "for i in range(0,9):\n",
- " t = names[3][i]\n",
- " names[3] = names[3][0:i] + names[2][i] + names[3][i+1:]\n",
- " names[2] = names[2][0:i] + t + names[2][i+1:]\n",
- " \n",
- "\n",
- "#Result \n",
- "print \"New: \", names[2], names[3] "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Original: raman\u0000\u0000\u0000\u0000 srinivas\u0000\n",
- "New: srinivas\u0000 raman\u0000\u0000\u0000\u0000\n"
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Exchange Names using Array of Pointers to Strings, Page number: 349<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#Variable declaration\n",
- "names = [\"akshay\\0\\0\\0\",\"parag\\0\\0\\0\\0\",\"raman\\0\\0\\0\\0\",\"srinivas\\0\",\"gopal\\0\\0\\0\\0\",\"rajesh\\0\\0\\0\"]\n",
- "\n",
- "#Initial condition\n",
- "print \"Original: \", names[2], names[3] \n",
- "\n",
- "#Exchanging names\n",
- "temp = names[2] \n",
- "names[2] = names[3] \n",
- "names[3] = temp \n",
- "\n",
- "#Result \n",
- "print \"New: \", names[2], names[3] \n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Original: raman\u0000\u0000\u0000\u0000 srinivas\u0000\n",
- "New: srinivas\u0000 raman\u0000\u0000\u0000\u0000\n"
- ]
- }
- ],
- "prompt_number": 12
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Solution, Page number: 351<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "import copy\n",
- "\n",
- "#Variable declaration\n",
- "names = []\n",
- "n=[\"John\",\"Max\",\"Jim\",\"Tony\",\"Tom\",\"Harry\"]\n",
- "\n",
- "for i in range(0,6):\n",
- " n1 = n[i]\n",
- " p = copy.copy(n1)\n",
- " names.append(p) \n",
- "\n",
- "for i in range(0,6):\n",
- " print names[i] \n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "John\n",
- "Max\n",
- "Jim\n",
- "Tony\n",
- "Tom\n",
- "Harry\n"
- ]
- }
- ],
- "prompt_number": 15
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [],
- "language": "python",
- "metadata": {},
- "outputs": []
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:4f09be4d67c9b4aa6b1f97f3788fa85904e9dfc6d52d9f340d60a59f47f71089" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h1>Chapter 9: Puppetting On Strings <h1>" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Print String , Page number: 329<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#Variable declaration\n", + "name= \"Klinsman\" # character array or string\n", + "i = 0\n", + "\n", + "#while loop for printing\n", + "while i <= 7 :\n", + " print name[i] \n", + " i = i + 1\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "K\n", + "l\n", + "i\n", + "n\n", + "s\n", + "m\n", + "a\n", + "n\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Print String, Page number: 330<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#Variable declaration\n", + "name= \"Klinsman\\0\" # character array or string\n", + "i = 0\n", + "\n", + "#while loop for printing\n", + "while (name[i] != '\\0') :\n", + " print name[i]\n", + " i = i + 1\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "K\n", + "l\n", + "i\n", + "n\n", + "s\n", + "m\n", + "a\n", + "n\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Print String Using Pointer, Page number: 330<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#Variable declaration\n", + "name = \"Klinsman\\0\" # string or character array\n", + "i = 0\n", + "ptr = name[i] # store base address of string\n", + "\n", + "#Result\n", + "while ptr != '\\0':\n", + " print ptr\n", + " i = i+1\n", + " ptr = name[i]\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "K\n", + "l\n", + "i\n", + "n\n", + "s\n", + "m\n", + "a\n", + "n\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>strlen Function, Page number: 336<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "#Variable declaration\n", + "arr = \"Bamboozled\" # character array or string\n", + "\n", + "#string length function\n", + "len1 = len(arr) \n", + "len2 = len( \"Humpty Dumpty\" )\n", + "\n", + "#Result\n", + "print \"string = %s length = %d\" %( arr, len1 )\n", + "print \"string = %s length = %d\" %(\"Humpty Dumpty\", len2 )\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "string = Bamboozled length = 10\n", + "string = Humpty Dumpty length = 13\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>xstrlen Function, Page number: 337<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#Function definition\n", + "def xstrlen(s):\n", + " length = 0\n", + " while ( s[length] != '\\0' ):\n", + " length += 1\n", + " return length \n", + "\n", + "#Variable declaration\n", + "arr = \"Bamboozled\\0\" # character array or string\n", + "\n", + "#Function calls\n", + "len1 = xstrlen(arr) \n", + "len2 = xstrlen( \"Humpty Dumpty\\0\" )\n", + "\n", + "#Result\n", + "print \"string = %s length = %d\" %( arr, len1 )\n", + "print \"string = %s length = %d\" %(\"Humpty Dumpty\\0\", len2 )\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "string = Bamboozled\u0000 length = 10\n", + "string = Humpty Dumpty\u0000 length = 13\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>strcpy Function, Page number: 338<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "#Variable declaration\n", + "source = \"Sayonara\" \n", + "target = [] \n", + "\n", + "#strcpy function\n", + "import copy\n", + "target = copy.copy(source)\n", + "\n", + "#Result\n", + "print \"source string = \", source \n", + "print \"target string = \", target \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "source string = Sayonara\n", + "target string = Sayonara\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>xstrcpy Function, Page number: 339<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#Function definition\n", + "def xstrcpy (t,s):\n", + " i = 0\n", + " while ( s[i] != '\\0' ):\n", + " t = t + s[i]\n", + " i = i + 1\n", + " return t\n", + " \n", + "#Variable declaration\n", + "source = \"Sayonara\\0\" \n", + "target = ''\n", + "\n", + "target = xstrcpy ( target, source ) # function call\n", + "\n", + "#Result\n", + "print \"source string = \", source \n", + "print \"target string = \", target " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "source string = Sayonara\u0000\n", + "target string = Sayonara\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>strcat Function, Page number: 342<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#Variable declaration\n", + "source = \"Folks!\" \n", + "target = \"Hello\"\n", + " \n", + "target = target + source # string concatenation\n", + "#Result\n", + "print \"source string = \", source \n", + "print \"target string = \", target \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "source string = Folks!\n", + "target string = HelloFolks!\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>strcmp Function, Page number: 343<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#Variable declaration\n", + "string1 = \"Jerry\\0\" \n", + "string2 = \"Ferry\\0\"\n", + "\n", + "#Function definition\n", + "def strcmp (string1 , string2):\n", + " if (string1 == string2):\n", + " v = 0 #If the two strings are identical, strcmp returns a value zero\n", + " return v\n", + " else:\n", + " n = 0\n", + " while ( string1[n]):\n", + " if ( string1[n] == string2[n]):\n", + " n = n + 1\n", + " continue\n", + " else:\n", + " v = ord(string1[n]) - ord(string2[n]) #returns the numeric difference between the ASCII values of the first non-matching pairs of characters\n", + " return v\n", + " return v\n", + "\n", + "#Function call\n", + "i = strcmp ( string1, \"Jerry\\0\" ) \n", + "j = strcmp ( string1, string2 ) \n", + "k = strcmp ( string1, \"Jerry boy\\0\" )\n", + "\n", + "#Result\n", + "print i,j,k" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 4 -32\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Two Dimensional Array of Characters, Page number: 344<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "\n", + "#Function definition\n", + "def strcmp (string1 , string2):\n", + " if (string1 == string2):\n", + " v = 0 #If the two strings are identical, strcmp returns a value zero\n", + " return v\n", + " else:\n", + " n = 0\n", + " while ( string1[n]):\n", + " if ( string1[n] == string2[n]):\n", + " n = n + 1\n", + " continue\n", + " else:\n", + " v = ord(string1[n]) - ord(string2[n]) #returns the numeric difference between the ASCII values of the first non-matching pairs of characters\n", + " return v\n", + " return v\n", + "\n", + "#Variable declaration\n", + "FOUND = 1\n", + "NOTFOUND = 0\n", + "masterlist =[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n", + "yourname = []\n", + "flag = NOTFOUND\n", + "\n", + "#Input from user\n", + "#yourname = raw_input(\"Enter your name: \")\n", + "yourname = \"Akshatha\"\n", + "\n", + "#Checking in the master list\n", + "for i in range(0,6):\n", + " a = strcmp ( masterlist[i], yourname )\n", + " if a == 0:\n", + " print \"Welcome, you can enter the palace\" \n", + " flag = FOUND\n", + " break\n", + " \n", + "if flag == NOTFOUND :\n", + " print \"Sorry, you are a trespasser\" " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry, you are a trespasser\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Exchange Names using 2-D Array of Characters, Page number: 348<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#Variable declaration\n", + "names = [\"akshay\\0\\0\\0\",\"parag\\0\\0\\0\\0\",\"raman\\0\\0\\0\\0\",\"srinivas\\0\",\"gopal\\0\\0\\0\\0\",\"rajesh\\0\\0\\0\"]\n", + "\n", + "#Initial condition\n", + "print \"Original: \", names[2], names[3] \n", + "\n", + "#Exchanging names\n", + "for i in range(0,9):\n", + " t = names[3][i]\n", + " names[3] = names[3][0:i] + names[2][i] + names[3][i+1:]\n", + " names[2] = names[2][0:i] + t + names[2][i+1:]\n", + " \n", + "\n", + "#Result \n", + "print \"New: \", names[2], names[3] " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Original: raman\u0000\u0000\u0000\u0000 srinivas\u0000\n", + "New: srinivas\u0000 raman\u0000\u0000\u0000\u0000\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Exchange Names using Array of Pointers to Strings, Page number: 349<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#Variable declaration\n", + "names = [\"akshay\\0\\0\\0\",\"parag\\0\\0\\0\\0\",\"raman\\0\\0\\0\\0\",\"srinivas\\0\",\"gopal\\0\\0\\0\\0\",\"rajesh\\0\\0\\0\"]\n", + "\n", + "#Initial condition\n", + "print \"Original: \", names[2], names[3] \n", + "\n", + "#Exchanging names\n", + "temp = names[2] \n", + "names[2] = names[3] \n", + "names[3] = temp \n", + "\n", + "#Result \n", + "print \"New: \", names[2], names[3] \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Original: raman\u0000\u0000\u0000\u0000 srinivas\u0000\n", + "New: srinivas\u0000 raman\u0000\u0000\u0000\u0000\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>Solution, Page number: 351<h3>" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "import copy\n", + "\n", + "#Variable declaration\n", + "names = []\n", + "n=[\"John\",\"Max\",\"Jim\",\"Tony\",\"Tom\",\"Harry\"]\n", + "\n", + "for i in range(0,6):\n", + " n1 = n[i]\n", + " p = copy.copy(n1)\n", + " names.append(p) \n", + "\n", + "for i in range(0,6):\n", + " print names[i] \n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "John\n", + "Max\n", + "Jim\n", + "Tony\n", + "Tom\n", + "Harry\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] }
\ No newline at end of file |