diff options
Diffstat (limited to '_Programming_With_C/chapter10.ipynb')
-rw-r--r-- | _Programming_With_C/chapter10.ipynb | 268 |
1 files changed, 268 insertions, 0 deletions
diff --git a/_Programming_With_C/chapter10.ipynb b/_Programming_With_C/chapter10.ipynb new file mode 100644 index 00000000..083baffa --- /dev/null +++ b/_Programming_With_C/chapter10.ipynb @@ -0,0 +1,268 @@ +{ + "metadata": { + "name": "chapter10" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h1>Chapter 10: Strings<h1> " + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 10.3, Page Number: 10.3<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "st='Programming'\nprint 'The string is %s' %st", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The string is Programming\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 10.4, Page Number: 10.4<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "st='Hello World'\nprint 'The line is :'\nprint st", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The line is :\nHello World\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 10.6, Page Number: 10.5<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#display the string character by character\n\nstrin='Welcome to python'\nfor i in strin:\n print i,", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "W e l c o m e t o p y t h o n\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 10.7, Page Number: 10.6<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "# to demonstrate the use of operators related function\n\nstr1='Programming'\nlen1=len(str1)\nprint 'The length of the string is ',len1\n\nstr2=str1\nprint 'First string is %s and copied string is %s' %(str1,str2)\n\nstr3='Computer'\n\nif str1==str3:\n print 'Both strings are equal'\nelif str1<str2:\n print 'First string is lesser than second string'\nelse:\n print 'First string is greater than second string'\n \ntempstr=' with C'\nstr1=str1+tempstr\nprint 'The concated string is ',str1\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The length of the string is 11\nFirst string is Programming and copied string is Programming\nFirst string is greater than second string\nThe concated string is Programming with C\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 10.8,Page Number: 10.7<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "# user defined function for string length and string copy\n\ndef strlength(str1):\n count=0\n for i in str1:\n count+=1\n \n return count\ndef strcopy(src):\n dst=[]\n for i in src:\n dst.append(i)\n \n dst=''.join(dst)\n \n return dst\n\n\nstr1='New Delhi'\nlen1=strlength(str1)\nprint 'The length of the string is ',len1\n\nstr2=strcopy(str1)\nprint 'First string is %s and copied string is %s' %(str1,str2)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The length of the string is 9\nFirst string is New Delhi and copied string is New Delhi\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 10.9,Page Number: 10.9<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "# A user defined implementation of comparison of two strings\n\ndef strcompare(str1,str2):\n \n len1=len(str1)\n len2=len(str2)\n \n if len1<len2:\n length=len1\n else:\n length=len2\n \n for i in xrange(0,length):\n if str1[i]<str2[i]:\n return -1\n elif str1[i]>str2[i]:\n return 1\n \n return 0\n\nstr1='Programming'\nstr2='Computer'\nstatus=strcompare(str1,str2)\nif status==-1:\n print 'First string is lesser than second string'\nelif status==1:\n print 'First string is greater than second string'\nelse:\n print 'Both strings ae equal'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "First string is greater than second string\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 10.10, Page Number: 10.10<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "# Left as well as right concatenation of two strings\n\ndef leftconcat(dst,src):\n dst=src+dst\n return dst\n\ndef rightconcat(dst,src):\n dst=dst+src\n return dst\n\nstr1='Hello'\nstr2='Friends'\n\ntempstr=leftconcat(str2,str1)\nprint 'The first string after left concatenation becomes ', tempstr\n\ntempstr=rightconcat(str2,str1)\nprint 'The first string after right concatenation becomes', tempstr\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The first string after left concatenation becomes HelloFriends\nThe first string after right concatenation becomes FriendsHello\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 10.11,Page Numbr: 10.12<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "# program to count total number of vowel in a given text\n\nstr1='All good boys have bread'\ncount=0\n\nfor i in str1:\n if i=='a' or i=='e'or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=='U' :\n count+=1\n \nprint 'Total number of vowels in a given text are ',count", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Total number of vowels in a given text are 8\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 10.12, Page Number: 10.13<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "# Character arithmetic examples\n\nch1='A'\nch2=ord(ch1)+3\nprint chr(ch2)\n\nch1=chr(ord(ch1)+1)\nprint ch1\n\nprint ord('a')\nprint ord('l')\n\nval=ord(ch1)*ch2\nprint val\n\nprint chr(100)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "D\nB\n97\n108\n4488\nd\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 10.13, Page Number: 10.13<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "# Program to convert lowercase alphabets to uppercase in a given text\n\ntext='I am studying 6 Theory Papers & 4 practicals'\n\nlen1=len(text)\ntext=list(text)\nfor i in xrange(0,len1):\n if text[i]>='a' and text[i]<='z':\n text[i]=chr(ord(text[i])+ord('A')-ord('a'))\n \n \ntext=''.join(text)\n\nprint 'The text after converting lowercase alphabets to uppercase is '\nprint text", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The text after converting lowercase alphabets to uppercase is \nI AM STUDYING 6 THEORY PAPERS & 4 PRACTICALS\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 10.14, Page Number: 10.14<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "# Program to search for a substring in a given string\n\ntext='The programming is a systematic process'\nsubstr='pro'\ntext_len=len(text)\nsub_len=len(substr)\ntext=list(text)\nsubstr=list(substr)\n\nfor i in xrange(0,text_len-sub_len+1):\n \n for j in xrange(0,sub_len):\n \n if text[i+j]==substr[j]:\n continue\n else:\n break\n \n if j==sub_len-1:\n print 'The substring is present from subscript %d onwards' %i\n \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The substring is present from subscript 4 onwards\nThe substring is present from subscript 32 onwards\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": "<h3>Example 10.15,Page number: 10.15<h3>" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "# Reordering a list of strings\n\n\ndef reorder(x):\n\n n=len(x)\n for item in range(0,n-1):\n for i in range(item+1,n):\n if x[item]>x[i]:\n temp=x[item]\n x[item]=x[i]\n x[i]=temp\n\n\n return\n\nx=['PACIFIC','ATLANTIC','INDIAN','CARIBBEAN','BERING','BLACK','RED','NORTH','BALTIC','CASPIAN']\nprint 'Original list of strings :\\n\\n'\n\nfor i in x:\n print \"String : \",i\n\nreorder(x)\n\nprint \"\\nReodered list of strings : \\n\\n\"\n\nfor i in x:\n print \"String : \",i\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Original list of strings :\n\n\nString : PACIFIC\nString : ATLANTIC\nString : INDIAN\nString : CARIBBEAN\nString : BERING\nString : BLACK\nString : RED\nString : NORTH\nString : BALTIC\nString : CASPIAN\n\nReodered list of strings : \n\n\nString : ATLANTIC\nString : BALTIC\nString : BERING\nString : BLACK\nString : CARIBBEAN\nString : CASPIAN\nString : INDIAN\nString : NORTH\nString : PACIFIC\nString : RED\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 15 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |