diff options
Diffstat (limited to 'ANSI_C_Programming_by_Yashwant_Kanetkar/chapter9.ipynb')
-rwxr-xr-x | ANSI_C_Programming_by_Yashwant_Kanetkar/chapter9.ipynb | 958 |
1 files changed, 958 insertions, 0 deletions
diff --git a/ANSI_C_Programming_by_Yashwant_Kanetkar/chapter9.ipynb b/ANSI_C_Programming_by_Yashwant_Kanetkar/chapter9.ipynb new file mode 100755 index 00000000..54eec269 --- /dev/null +++ b/ANSI_C_Programming_by_Yashwant_Kanetkar/chapter9.ipynb @@ -0,0 +1,958 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:d543de519ea53ce295e62c624f3b779f3394a58f64540659aef438fffab60f98" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 9: PUPPETTING ON STRINGS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:311" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "name=\"Klinsman\"\n", + "i=0\n", + "while i<=7:\n", + " print \"%c\" % (name[i]), #method of printing without line feed\n", + " i+=1\n", + "print \"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "K l i n s m a n \n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:311-312" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "name=\"Klinsman \"\n", + "i=0\n", + "while name[i]!=' ':\n", + " print \"%c\" % (name[i]), \n", + " i+=1\n", + "print \"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " K l i n s m a n \n", + "\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:312" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "name=\"Klinsman \"\n", + "ptr=[]\n", + "ptr=name #store base address of string\n", + "i=0\n", + "while name[i]!=' ':\n", + " print \"%c\" % (ptr[i]), \n", + " i+=1\n", + "print \"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "K l i n s m a n \n", + "\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:313" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "name=\"Klinsman\"\n", + "print \"%s\" % (name)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Klinsman\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:313" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter your name\"\n", + "name=raw_input()\n", + "print \"Hello %s!\\n\" % (name)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Debashish\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello Debashish!\n", + "\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:314" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter your full name:\"\n", + "name=raw_input()\n", + "print \"Hello!\"\n", + "print name" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your full name:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Debashish Roy\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello!\n", + "Debashish Roy\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:315" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "str1=\"Hello\"\n", + "s=\"Good Morning\"\n", + "str2=str1 #No error in Python since all variables are by default pointers\n", + "q=s" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:316" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "str1=\"Hello\"\n", + "p=\"Hello\"\n", + "str1=\"Bye\" #No error in python\n", + "p=\"Bye\"" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:317" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "arr=\"Bamboozled\"\n", + "len1=len(arr) #return length of string\n", + "len2=len(\"Humpty Dumpty\")\n", + "print \"string=%s length=%d\\n\" % (arr,len1)\n", + "print \"string=%s length=%d\\n\" % (\"Humpty Dumpty\",len2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "string=Bamboozled length=10\n", + "\n", + "string=Humpty Dumpty length=13\n", + "\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:317-318" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def xstrlen(s):\n", + " length=0\n", + " for item in s:\n", + " if s!=\"\\0\":\n", + " length+=1\n", + " return length\n", + "arr=\"Bamboozled\"\n", + "len1=xstrlen(arr) \n", + "len2=xstrlen(\"Humpty Dumpty\")\n", + "print \"string=%s length=%d\\n\" % (arr,len1)\n", + "print \"string=%s length=%d\\n\" % (\"Humpty dumpty\",len2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "string=Bamboozled length=10\n", + "\n", + "string=Humpty dumpty length=13\n", + "\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:319" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "import copy\n", + "source=\"Sayonara\"\n", + "target=copy.copy(source) #copy string\n", + "print \"source string=%s\\n\" % (source)\n", + "print \"target string=%s\\n\" % (target)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "source string=Sayonara\n", + "\n", + "target string=Sayonara\n", + "\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:319-320" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def xstrcpy(t,s):\n", + " t=\"\\0\"\n", + " for item in s:\n", + " t=t+str(item)\n", + " return t\n", + "source=\"Sayonara\"\n", + "target=[]\n", + "target=xstrcpy(target,source)\n", + "print \"source string=%s\\n\" % (source)\n", + "print \"target string=%s\\n\" % (target)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "source string=Sayonara\n", + "\n", + "target string=\u0000Sayonara\n", + "\n" + ] + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:321" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "pi=3.14\n", + "print \"Enter radius of circle\"\n", + "r=eval(raw_input())\n", + "a=pi*r*r\n", + "print \"Area of circle=%f\\n\" % (a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter radius of circle\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area of circle=28.260000\n", + "\n" + ] + } + ], + "prompt_number": 26 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:322" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "source=\"Folks!\"\n", + "target=\"Hello\"\n", + "target=target+source\n", + "print \"source string=%s\\n\" % (source)\n", + "print \"target string=%s\\n\" % (target)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "source string=Folks!\n", + "\n", + "target string=HelloFolks!\n", + "\n" + ] + } + ], + "prompt_number": 27 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:322-323" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "string1=\"Jerry\"\n", + "string2=\"Ferry\"\n", + "i=cmp(string1,\"Jerry\")\n", + "j=cmp(string1,string2)\n", + "k=cmp(string1,\"Jerry boy\")\n", + "print \"%d %d %d\\n\" % (i,j,k)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 1 -1\n", + "\n" + ] + } + ], + "prompt_number": 29 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:323-324" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def FOUND():\n", + " return 1\n", + "def NOTFOUND():\n", + " return 0\n", + "masterlist=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n", + "print \"Enter your name\"\n", + "yourname=raw_input()\n", + "flag=NOTFOUND()\n", + "for i in range(0,6,1):\n", + " a=cmp(masterlist[i],yourname)\n", + " if a==0:\n", + " print \"Welcome,you can enter the palace\\n\"\n", + " flag=FOUND()\n", + " break\n", + "if flag==NOTFOUND():\n", + " print \"Sorry,you are a trespasser\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "gopal\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Welcome,you can enter the palace\n", + "\n" + ] + } + ], + "prompt_number": 51 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:327" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "names=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n", + "names = map(bytearray, names)\n", + "print \"Original:%s %s\\n\" % (names[2],names[3])\n", + "s = sorted((names[2], names[3]), key=len)\n", + "for i in range(len(s[1])):\n", + " try:\n", + " t=s[0][i]\n", + " s[0][i]=s[1][i]\n", + " s[1][i]=t\n", + " except IndexError:\n", + " for _ in range(i, len(s[1])):\n", + " #remove the items from the longer string to and append them to the shorter one.\n", + " s[0].append(s[1].pop(i)) \n", + "print \"New:%s %s\\n\" % (names[2],names[3])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Original:raman srinivas\n", + "\n", + "New:srinivas raman\n", + "\n" + ] + } + ], + "prompt_number": 46 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE-328" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "names=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n", + "print \"Original:%s %s\\n\" % (names[2],names[3])\n", + "t=names[2]\n", + "names[2]=names[3]\n", + "names[3]=t\n", + "print \"New:%s %s\\n\" % (names[2],names[3])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Original:raman srinivas\n", + "\n", + "New:srinivas raman\n", + "\n" + ] + } + ], + "prompt_number": 48 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:329" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "names=[]\n", + "for i in range(6):\n", + " names.append(\"\\0\") \n", + "for i in range(0,6,1):\n", + " print \"Enter name\"\n", + " names[i]=raw_input()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "RAM\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "GOPAL\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "PANDEY\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "GOPU\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "CHOTU\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "RADHEY\n" + ] + } + ], + "prompt_number": 50 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:329-330" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "names=[]\n", + "for i in range(0,6):\n", + " names.append('\\0')\n", + "for i in range(0,6,1):\n", + " print \"Enter name\"\n", + " n=raw_input()\n", + " length=len(n)\n", + " import copy\n", + " p=copy.copy(n)\n", + " names[i]=p\n", + "for i in range(0,6,1):\n", + " print \"%s\\n\" % (names[i])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "MUKUT\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "BIHARI\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "PANDEY\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "MADHUBALA\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "PANDEY\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "SUNITA\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "MUKUT\n", + "\n", + "BIHARI\n", + "\n", + "PANDEY\n", + "\n", + "MADHUBALA\n", + "\n", + "PANDEY\n", + "\n", + "SUNITA\n", + "\n" + ] + } + ], + "prompt_number": 7 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |