diff options
Diffstat (limited to 'Programming_in_C_using_ANSI_C/KamthaneChapter8.ipynb')
-rwxr-xr-x | Programming_in_C_using_ANSI_C/KamthaneChapter8.ipynb | 2750 |
1 files changed, 0 insertions, 2750 deletions
diff --git a/Programming_in_C_using_ANSI_C/KamthaneChapter8.ipynb b/Programming_in_C_using_ANSI_C/KamthaneChapter8.ipynb deleted file mode 100755 index 3243f3f8..00000000 --- a/Programming_in_C_using_ANSI_C/KamthaneChapter8.ipynb +++ /dev/null @@ -1,2750 +0,0 @@ -{
- "metadata": {
- "name": ""
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h1>Chapter 8: Working with Strings & Standard Functions<h1>"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.1, Page number: 236<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Display string without considering NULL character\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "name1 = 'SANJAY'\n",
- "#There is no null termination character in python\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"Name1 = %s\"%(name1))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Name1 = SANJAY"
- ]
- }
- ],
- "prompt_number": 23
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.2, Page number: 236<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Display successive strings\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "name1 = 'SANJAY'\n",
- "name2 = 'SANJAY'\n",
- "#There is no null termination character in python\n",
- "\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"Name1 = %s\"%name1)\n",
- "sys.stdout.write(\"\\nName2 = %s\"%(name2))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Name1 = SANJAY\n",
- "Name2 = SANJAY"
- ]
- }
- ],
- "prompt_number": 21
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.3, Page number: 237<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Print \"WELCOME\" by using different formats of initialization \n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "#in python, string initialization can be done using single quotes and double quotes\n",
- "arr1 = 'WELCOME'\n",
- "arr2 = \"WELCOME\"\n",
- "arr3 = 'WELCOME'\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"\\nArray1 = %s\"%(arr1))\n",
- "sys.stdout.write(\"\\nArray2 = %s\"%(arr2))\n",
- "sys.stdout.write(\"\\nArray3 = %s\"%(arr3))\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Array1 = WELCOME\n",
- "Array2 = WELCOME\n",
- "Array3 = WELCOME"
- ]
- }
- ],
- "prompt_number": 24
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.4, Page number: 238<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Display the string 'PRABHAKAR' using various format specifications\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "text = \"PRABHAKAR\"\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"%s\\n\"%(text))\n",
- "sys.stdout.write(\"%.5s\\n\"%(text))\n",
- "sys.stdout.write(\"%.8s\\n\"%(text))\n",
- "sys.stdout.write(\"%.15s\\n\"%(text))\n",
- "sys.stdout.write(\"%-10.4s\\n\"%(text))\n",
- "sys.stdout.write(\"%11s\\n\"%(text))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "PRABHAKAR\n",
- "PRABH\n",
- "PRABHAKA\n",
- "PRABHAKAR\n",
- "PRAB \n",
- " PRABHAKAR\n"
- ]
- }
- ],
- "prompt_number": 25
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.5, Page number: 239<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Print the elements of the character array using while loop.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "text = \"HAVE A NICE DAY\"\n",
- "i = 0\n",
- "\n",
- "#Result\n",
- "#There is no null character for strings in python\n",
- "\n",
- "while i < 15:\n",
- " sys.stdout.write(\"%c\"%(text[i]))\n",
- " i += 1"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "HAVE A NICE DAY"
- ]
- }
- ],
- "prompt_number": 27
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.6, Page number: 239<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Print the elements of the character array.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "text = \"HAVE A NICE DAY\"\n",
- "i = 0\n",
- "\n",
- "#Result\n",
- "#There is no null character for strings in python\n",
- "\n",
- "while i < 15:\n",
- " sys.stdout.write(\"%c\"%(text[i]))\n",
- " i += 1"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "HAVE A NICE DAY"
- ]
- }
- ],
- "prompt_number": 28
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.7, Page number: 241<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Count the number of characters in a given string\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "text = raw_input(\"Type Text Below\")\n",
- "\n",
- "#Calculation\n",
- "len1 = len(text) #since len in a built in function in python, len1 is used\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"Length of String = %d\"%(len1))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Type Text BelowHello\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Length of String = 5"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.8, Page number: 241<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Determine the length of the string\n",
- "#and find its equivalent ASCII codes.\n",
- "\n",
- "import sys\n",
- "\n",
- "\n",
- "#Variable Initialization\n",
- "name = raw_input(\"Enter your name : \")\n",
- "l = len(name)\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"Your Name is %s & \"%(name))\n",
- "sys.stdout.write(\"it contains %d characters\"%(l))\n",
- "sys.stdout.write(\"\\n\\nName & it's Ascii Equivalent.\\n\")\n",
- "sys.stdout.write(\"=====================================\\n\")\n",
- "\n",
- "for i in range(0,l):\n",
- " sys.stdout.write(\"\\n%c\\t\\t%d\"%(name[i],ord(name[i]))) #ord() function is used to get the ASCII value of a character"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter your name : SACHIN\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Your Name is SACHIN & it contains 6 characters\n",
- "\n",
- "Name & it's Ascii Equivalent.\n",
- "=====================================\n",
- "\n",
- "S\t\t83\n",
- "A\t\t65\n",
- "C\t\t67\n",
- "H\t\t72\n",
- "I\t\t73\n",
- "N\t\t78"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.9, Page number: 242<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Remove the occurrences of 'The' word from entered text\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "j = 0\n",
- "line = raw_input(\"Enter Text Below.\")\n",
- "l = len(line)\n",
- "line2 = ['0' for i in range(0,80)]\n",
- "i = 0\n",
- "\n",
- "#Calculation\n",
- "while i < l:\n",
- " if (i >= l-4 or l ==3) and (line[l-4] == ' ' and line[l-3] == 't' and line[l-2] == 'h' and line[l-1] == 'e'):\n",
- " continue\n",
- " if line[i] == 't' and line[i+1] == 'h' and line[i+2] == 'e' and line[i+3] == ' ':\n",
- " i += 3\n",
- " continue\n",
- " else:\n",
- " line2[j] = line[i]\n",
- " j += 1\n",
- " i += 1\n",
- " \n",
- "#Result\n",
- "sys.stdout.write(\"\\nText With 'The' -: %s\"%(line))\n",
- "sys.stdout.write(\"\\nText Without 'The' -: \")\n",
- "for l in range(0,j):\n",
- " sys.stdout.write(\"%c\"%(line2[l]))\n",
- " "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Text Below.Printf write data to the screen.\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Text With 'The' -: Printf write data to the screen.\n",
- "Text Without 'The' -: Printf write data to screen."
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.10, Page number: 243<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Delete all the occurrences of vowels in a given text.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "j = 0\n",
- "line = raw_input(\"Enter Text Below.\")\n",
- "\n",
- "#Calculation\n",
- "for i in range(0,len(line)):\n",
- " if line[i] == 'a' or line[i] == 'e' or line[i] == 'i' or line[i] == 'o' or line[i] == 'u':\n",
- " continue\n",
- " else:\n",
- " line2[j] = line[i]\n",
- " j += 1\n",
- " \n",
- "#Result\n",
- "sys.stdout.write(\"Text With Vowels -: %s\"%(line))\n",
- "sys.stdout.write(\"\\nText Without Vowels -: \")\n",
- "for l in range(0,j): #There is no null terminating character in python, so we have to specify length\n",
- " sys.stdout.write(\"%c\"%(line2[l])) \n",
- " "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Text Below.Have a nice day.\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Text With Vowels -: Have a nice day.\n",
- "Text Without Vowels -: Hv nc dy."
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.11, Page number: 244<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Find the length of characteres in a given string including and excluding spaces.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "i = 0\n",
- "len1 = 0 #Since len is a built in function in python, len1 is used\n",
- "ex = 0\n",
- "\n",
- "text = raw_input(\"Enter Text Here : \")\n",
- "\n",
- "#Calculation\n",
- "while i != len(text)-1: #There is no null terminating character in python.\n",
- " if text[i] == ' ':\n",
- " ex += 1\n",
- " else:\n",
- " len1 += 1\n",
- " i += 1\n",
- " \n",
- "#Result\n",
- "sys.stdout.write(\"\\nLengh of String Including Spaces : %d\"%(len1+ex))\n",
- "sys.stdout.write(\"\\nLengh of String Excluding Spaces : %d\"%(len1))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Text Here : Time is Money.\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Lengh of String Including Spaces : 13\n",
- "Lengh of String Excluding Spaces : 11"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.12, Page number: 245<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Display the length of entered string and display it as per output shown.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "string = raw_input(\"Enter a String\")\n",
- "ln = len(string)\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"\\nLength of Given String : %d\"%(ln))\n",
- "sys.stdout.write(\"\\nSequence of Characters Displayed on Screen\")\n",
- "sys.stdout.write(\"\\n======== == ========== ========= == ======\")\n",
- "sys.stdout.write(\"\\n\\n\")\n",
- "\n",
- "for c in range(0,ln-2+1):\n",
- " d = c + 1\n",
- " sys.stdout.write(\"\\t%.*s\\n\"%(d,string))\n",
- "\n",
- "for c in range(ln,0-1,-1):\n",
- " d = c + 1\n",
- " sys.stdout.write(\"\\t%.*s\\n\"%(d,string))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a StringHAPPY\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Length of Given String : 5\n",
- "Sequence of Characters Displayed on Screen\n",
- "======== == ========== ========= == ======\n",
- "\n",
- "\tH\n",
- "\tHA\n",
- "\tHAP\n",
- "\tHAPP\n",
- "\tHAPPY\n",
- "\tHAPPY\n",
- "\tHAPP\n",
- "\tHAP\n",
- "\tHA\n",
- "\tH\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.13, Page number: 247<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Copy contents of one string to another string by using strcpy()\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "ori = raw_input(\"Enter Your Name : \")\n",
- "\n",
- "#Process\n",
- "dup = ori #In python, string values can be simply copied using assignment operator\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"Original String : %s\"%(ori))\n",
- "sys.stdout.write(\"\\nDuplicate String : %s\"%(dup))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Your Name : SACHIN\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Original String : SACHIN\n",
- "Duplicate String : SACHIN"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.14, Page number: 247<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Copy contents of one string to another, without strcpy() function.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "ori = raw_input(\"Enter Your Name : \")\n",
- "\n",
- "#Calculation\n",
- "dup = ori #In python, string values can be simply copied using assignment operator\n",
- " \n",
- "#Result\n",
- "sys.stdout.write(\"Original String : %s\"%(ori))\n",
- "sys.stdout.write(\"\\nDuplicate String : %s\"%(dup))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Your Name : SACHIN\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Original String : SACHIN\n",
- "Duplicate String : SACHIN"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.15, Page number: 248<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Copy source string to destination string upto a specified length.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "str1 = raw_input(\"Enter Source String : \")\n",
- "str2 = raw_input(\"Enter Destination String : \")\n",
- "n = int(raw_input(\"Enter Number of Characters to Replace in Destination String :\"))\n",
- "\n",
- "#Calculation\n",
- "str2 = str1[:n] + str2[n:] #Since python strings are not mutable, first and second array elements to be added to make a new one\n",
- " \n",
- "sys.stdout.write(\"Source String -: %s\"%(str1))\n",
- "sys.stdout.write(\"\\nDestination String -: %s\"%(str2))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Source String : wonderful\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Destination String : beautiful\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Number of Characters to Replace in Destination String :6\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Source String -: wonderful\n",
- "Destination String -: wonderful"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.16, Page number: 249<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Compare the two string using stricmp() function. \n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "sr = raw_input(\"Enter String(1) :\")\n",
- "tar = raw_input(\"Enter String(2) :\")\n",
- "\n",
- "diff = (sr == tar) #in python, relational operators can be used directly\n",
- "\n",
- "if diff == 0:\n",
- " sys.stdout.write(\"The Two Strings are Identical.\")\n",
- "else:\n",
- " sys.stdout.write(\"The Tow Strings are Different\")"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter String(1) :HELLO\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter String(2) :hello\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "The Two Strings are Identical."
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.17, Page number: 250<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Perform following.\n",
- "\n",
- "# 1. Display the question \"What is the Unit of Traffic?\"\n",
- "# 2. Accept the Answer.\n",
- "# 3. If the answer is wrong (other than Earlang) display \"Try again!\" & continues to answer.\n",
- "# 4. Otherwise, if it is correct \"Earlang\" display the message \"Answer is correct\".\n",
- "# 5. If the user gives correct anser in first two attempts the program will terminate.\n",
- "# 6. If the user fails to provide correct answer in three attempts the program itself gives the answer.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Calculation & Result\n",
- "for i in range(0,3):\n",
- " ans = raw_input(\"What is the Unit of Traffic ?\")\n",
- " \n",
- " if ans == \"Earlang\":\n",
- " sys.stdout.write(\"\\nAnswer is Correct.\")\n",
- " else:\n",
- " if i < 3:\n",
- " sys.stdout.write(\"\\nTry Again !\\n\")\n",
- " \n",
- "if i == 3:\n",
- " sys.stdout.write(\"\\nUnit of Traffic is Earlang.\")"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "What is the Unit of Traffic ?Earlan\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Try Again !\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "What is the Unit of Traffic ?Earlam\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Try Again !\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "What is the Unit of Traffic ?Earlang\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Answer is Correct."
- ]
- }
- ],
- "prompt_number": 13
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.18, Page number: 252<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Compare two strings upto specified length.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "sr = raw_input(\"Enter String(1):\")\n",
- "tar = raw_input(\"Enter String(2):\")\n",
- "n = int(raw_input(\"Enter Lenght upto which comparison is to be made\"))\n",
- "\n",
- "#Calculation & Result\n",
- "diff = (sr == tar[:n])\n",
- "\n",
- "if diff == 0:\n",
- " sys.stdout.write(\"The Two Strings are Identical upto %d characters.\"%(n))\n",
- "else:\n",
- " sys.stdout.write(\"The Two Strings are Different\")\n",
- " "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter String(1):HELLO\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter String(2):HE MAN\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Lenght upto which comparison is to be made2\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "The Two Strings are Identical upto 2 characters."
- ]
- }
- ],
- "prompt_number": 14
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.19, Page number: 253<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Compare two strings and display the position in which it is different.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "diff = 0\n",
- "sr = raw_input(\"Enter String(1) :\")\n",
- "tar = raw_input(\"Enter String(2) :\")\n",
- "\n",
- "#Checking\n",
- "for i in range(0,len(sr)):\n",
- " if sr[i] != tar[i]:\n",
- " sys.stdout.write(\"%c %c\\n\"%(sr[i],tar[i]))\n",
- " diff += 1\n",
- " \n",
- "#Result\n",
- "if len(sr) == len(tar) and diff == 0:\n",
- " sys.stdout.write(\"\\nThe Two Strings are Identical\")\n",
- "else:\n",
- " sys.stdout.write(\"\\nThe Two Strings are Different at %d places.\"%(diff))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter String(1) :BEST LUCK\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter String(2) :GOOD LUCK\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "B G\n",
- "E O\n",
- "S O\n",
- "T D\n",
- "\n",
- "The Two Strings are Different at 4 places."
- ]
- }
- ],
- "prompt_number": 15
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.20, Page number: 254<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Convert upper case string to lower case using strlwr().\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "upper = raw_input(\"Enter a string in Upper case :\")\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"After strlwr() : %s\"%(upper.lower()))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a string in Upper case :ABCDEFG\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "After strlwr() : abcdefg"
- ]
- }
- ],
- "prompt_number": 16
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.21, Page number: 254<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Convert Lower case string to upper case using strupr()\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "upper = raw_input(\"Enter a string in Lower Case :\")\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"After strupr() : %s\"%(upper.upper()))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a string in Lower Case :abcdefg\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "After strupr() : ABCDEFG"
- ]
- }
- ],
- "prompt_number": 17
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.22, Page number: 255<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Display the duplicate string using strdup() function\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "text1 = raw_input(\"Enter Text : \")\n",
- "\n",
- "#Result\n",
- "text2 = text1 #in python, a string can be duplicated by just using the assignment operator\n",
- "\n",
- "sys.stdout.write(\"Original String = %s\\nDuplicate String = %s\"%(text1,text2))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Text : Today is a Good Day.\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Original String = Today is a Good Day.\n",
- "Duplicate String = Today is a Good Day."
- ]
- }
- ],
- "prompt_number": 18
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.23, Page number: 256<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Find first occurrence of a given character in a given string. \n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "string = raw_input(\"Enter Text Below :\")\n",
- "ch = raw_input(\"Character to find :\")\n",
- "\n",
- "#Result\n",
- "chp = string.count(ch)\n",
- "if chp:\n",
- " sys.stdout.write(\"Character %s found in string\"%(ch))\n",
- "else:\n",
- " sys.stdout.write(\"Character %s not found in string\"%(ch))\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Text Below :Hello Begineers.\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Character to find :r\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Character r found in string"
- ]
- }
- ],
- "prompt_number": 19
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.24, Page number: 257<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Find first occurrence of a given character in a given string and display the memory location. \n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "line1 = raw_input(\"Enter Text :\")\n",
- "line2 = raw_input(\"Enter Character to find from the text : \")\n",
- "\n",
- "for i in range(0,len(line1)):\n",
- " sys.stdout.write(\"%c %d\\n\"%(line1[i],id(line1[i])))\n",
- "\n",
- "chp = line1.count(line2)\n",
- "if chp:\n",
- " sys.stdout.write(\"\\nAddress of first %s returned by strchr() is %d\"%(line2,id(line1[chp])))\n",
- "else:\n",
- " sys.stdout.write(\"'%s' String is not present in Given String \"%(line2))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Text :HELLO\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Character to find from the text : L\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "H 20382328\n",
- "E 20384080\n",
- "L 19678272\n",
- "L 19678272\n",
- "O 20198416\n",
- "\n",
- "Address of first L returned by strchr() is 19678272"
- ]
- }
- ],
- "prompt_number": 20
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.25, Page number: 258<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Find the occurrence of second string in the first string.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "line1 = raw_input(\"Enter Line1 :\")\n",
- "line2 = raw_input(\"Enter Line2 :\")\n",
- "\n",
- "#Calculation & Result\n",
- "chp = line1.count(line2)\n",
- "\n",
- "if chp:\n",
- " sys.stdout.write(\"'%s' String is present in Given String.\"%(line2))\n",
- "else:\n",
- " sys.stdout.write(\"'%s' String is not present in Given String.\"%(line2))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Line1 :INDIA IS MY COUNTRY.\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Line2 :INDIA\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "'INDIA' String is present in Given String."
- ]
- }
- ],
- "prompt_number": 21
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.26, Page number: 259<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Append second string at the end of first string using strcat() function\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "text1 = raw_input(\"Enter Text1 :\")\n",
- "text2 = raw_input(\"Enter Text2 :\")\n",
- "\n",
- "#Calculation and Result\n",
- "text1 = text1 + \" \" + text2 #in python, '+' operator can also be used for string concatenation\n",
- "\n",
- "sys.stdout.write(\"%s\"%(text1))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Text1 :I Am\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Text2 :an Indian\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "I Am an Indian"
- ]
- }
- ],
- "prompt_number": 22
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.27, Page number: 259<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Concatenate two strings without use of standard functions.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "fname = raw_input(\"First Name :\")\n",
- "sname = raw_input(\"Second Name :\")\n",
- "lname = raw_input(\"Last Name :\")\n",
- "\n",
- "#Calculation\n",
- "name = fname + \" \" + sname + \" \" + lname\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"\\nComplete Name After Concatenation.\\n\")\n",
- "sys.stdout.write(\"%s\"%(name))\n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "First Name :MOHAN\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Second Name :KARAMCHAND\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Last Name :GANDHI\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Complete Name After Concatenation.\n",
- "MOHAN KARAMCHAND GANDHI"
- ]
- }
- ],
- "prompt_number": 23
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.28, Page number: 261<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Append second string with specified (n) number of characters at the end of first string.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "text1 = raw_input(\"Enter Text1 :\")\n",
- "text2 = raw_input(\"Enter Text2 :\")\n",
- "n = int(raw_input(\"Enter Number of Characters to Add :\"))\n",
- "\n",
- "#Calculation & Result\n",
- "text1 = text1 + \" \" + text2[:n]\n",
- "\n",
- "sys.stdout.write(\"%s\\n\"%(text1))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Text1 :MAY I\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Text2 :COME IN ?\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Number of Characters to Add :4\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "MAY I COME\n"
- ]
- }
- ],
- "prompt_number": 24
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.29, Page number: 261<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Reverse of the given string.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "text = raw_input(\"Enter String\")\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"Reverse String\\n\")\n",
- "text = text[::-1]\n",
- "#This is extended slice syntax. It works by doing [begin:end:step] \n",
- "#by leaving begin and end off and specifying a step of -1, it reverses a string.\n",
- "\n",
- "sys.stdout.write(\"%s\"%(text))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter StringABCDEFGHI\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Reverse String\n",
- "IHGFEDCBA"
- ]
- }
- ],
- "prompt_number": 25
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.30, Page number: 262<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Reverse of the given string.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "text = raw_input(\"Enter String\")\n",
- "i = 0\n",
- "\n",
- "#Result\n",
- "while i < len(text):\n",
- " sys.stdout.write(\"\\n%c is stored at location %d\"%(text[i],id(text[i])))\n",
- " i += 1\n",
- " \n",
- "sys.stdout.write(\"\\nReverse String :- \")\n",
- "text = text[::-1]\n",
- "#This is extended slice syntax. It works by doing [begin:end:step] \n",
- "#by leaving begin and end off and specifying a step of -1, it reverses a string.\n",
- "\n",
- "sys.stdout.write(\"%s\"%(text))\n",
- "\n",
- "i = 0\n",
- "while i < len(text):\n",
- " sys.stdout.write(\"\\n%c is stored at location %d\"%(text[i],id(text[i])))\n",
- " i += 1\n",
- "\n",
- "#Memory address differs in different systems"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter StringABC\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "A is stored at location 20383432\n",
- "B is stored at location 20198992\n",
- "C is stored at location 20199040\n",
- "Reverse String :- CBA\n",
- "C is stored at location 20199040\n",
- "B is stored at location 20198992\n",
- "A is stored at location 20383432"
- ]
- }
- ],
- "prompt_number": 26
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.31, Page number: 263<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Replace (set) given string with given symbol. \n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "string = raw_input(\"Enter String :\")\n",
- "symbol = raw_input(\"Enter Symbol for Replacement : \")\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"Before strset(): %s\\n\"%(string))\n",
- "string = symbol*len(string)\n",
- "\n",
- "sys.stdout.write(\"After strset() : %s\"%(string))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter String :LEARN C\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Symbol for Replacement : Y\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Before strset(): LEARN C\n",
- "After strset() : YYYYYYY"
- ]
- }
- ],
- "prompt_number": 27
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.32, Page number: 264<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Replace (set) given string with given symbol for given number of arguments.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "string = raw_input(\"Enter String :\")\n",
- "symbol = raw_input(\"Enter Symbol for Replacement : \")\n",
- "n = int(raw_input(\"How many String Character to be replaced.\"))\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"Before strset() : %s\\n\"%(string))\n",
- "\n",
- "string = symbol * n + string[n:]\n",
- "sys.stdout.write(\"After strset() : %s\\n\"%(string))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter String :ABCDEFGHIJ\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Symbol for Replacement : +\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "How many String Character to be replaced.4\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Before strset() : ABCDEFGHIJ\n",
- "After strset() : ++++EFGHIJ\n"
- ]
- }
- ],
- "prompt_number": 28
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.33, Page number: 265<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Display the position from which the two strings are different.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "stra = raw_input(\"First String : \")\n",
- "strb = raw_input(\"Second String : \")\n",
- "\n",
- "#There is no built in function for python to check upto which position the strings are equal.\n",
- "for length in range(min(len(stra), len(strb))):\n",
- " if stra[length] != strb[length]:\n",
- " break\n",
- "\n",
- "sys.stdout.write(\"After %d Characters there is no match.\"%(length))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "First String : GOOD MORNING\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Second String : GOOD BYE\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "After 5 Characters there is no match."
- ]
- }
- ],
- "prompt_number": 29
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.34, Page number: 266<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Print given string from first occurrence of given character.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "text1 = raw_input(\"Enter String : \")\n",
- "text2 = raw_input(\"Enter Character :\")\n",
- "\n",
- "for length in range(0,len(text1)):\n",
- " if text1[length] == text2[0]:\n",
- " break\n",
- " \n",
- "sys.stdout.write(\"String from given Character : %s\"%(text1[length:]))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter String : INDIA IS GREAT\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Character :G\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "String from given Character : GREAT"
- ]
- }
- ],
- "prompt_number": 30
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.35, Page number: 266<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Count a character that appears in a given text\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "i = 0\n",
- "count = 0\n",
- "text = raw_input(\"Type Text Below.\")\n",
- "find = raw_input(\"Type a character to count : \")\n",
- "\n",
- "#Calculation\n",
- "while i < len(text): #There is no null terminating character for python string\n",
- " if text[i] == find:\n",
- " count += 1\n",
- " i += 1\n",
- " \n",
- "#Result\n",
- "sys.stdout.write(\"\\nCharacter (%s) found in Given String = %d Times.\"%(find,count))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Type Text Below.Programming\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Type a character to count : m\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Character (m) found in Given String = 2 Times."
- ]
- }
- ],
- "prompt_number": 32
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.36, Page number: 267<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Count the character 'm' in a given string.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "count = 0\n",
- "text = raw_input(\"\") #it is easy to get string at once in python\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"%s\"%(text))\n",
- "\n",
- "for i in range(0,len(text)):\n",
- " if text[i] == '\\n':\n",
- " break\n",
- " else:\n",
- " if text[i] == 'm':\n",
- " count += 1\n",
- " \n",
- "sys.stdout.write(\"\\n\\nCharacter 'm' Found in Text = %d times.\"%(count))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Programming is a skill.\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Programming is a skill.\n",
- "\n",
- "Character 'm' Found in Text = 2 times."
- ]
- }
- ],
- "prompt_number": 33
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.37, Page number: 268<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Count the characters 'm', 'r', and 'o' that appears in a string without using any functions\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "text = \"Programming is good habit\"\n",
- "m = 0\n",
- "o = 0\n",
- "r = 0\n",
- "\n",
- "#Calculation\n",
- "for i in range(0,len(text)):\n",
- " if text[i] == 'm':\n",
- " m += 1\n",
- " if text[i] == 'r':\n",
- " r += 1\n",
- " if text[i] == 'o':\n",
- " o += 1\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"\\nCharacter 'm' Found in Text = %d times.\\n\"%(m))\n",
- "sys.stdout.write(\"\\nCharacter 'r' Found in Text = %d times.\\n\"%(r))\n",
- "sys.stdout.write(\"\\nCharacter 'o' Found in Text = %d times.\"%(o))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Character 'm' Found in Text = 2 times.\n",
- "\n",
- "Character 'r' Found in Text = 2 times.\n",
- "\n",
- "Character 'o' Found in Text = 3 times."
- ]
- }
- ],
- "prompt_number": 46
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.38, Page number: 269<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Copy contents of one string to another string\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "ori = raw_input(\"Enter Your Name :\")\n",
- "\n",
- "#Copying\n",
- "#Since strings in python are immutable, individual character assignment is not possible.\n",
- "#but assignment operator simply copies the entire string to another\n",
- "\n",
- "dup = ori\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"Original String : %s\"%(ori))\n",
- "sys.stdout.write(\"\\nDuplicate String : %s\"%(dup))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Your Name :SACHIN\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Original String : SACHIN\n",
- "Duplicate String : SACHIN"
- ]
- }
- ],
- "prompt_number": 34
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.39, Page number: 270<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Palindrome or not.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "i = 0\n",
- "str1 = raw_input(\"Enter the word : \") #Since str is a keyword in python, str1 is used\n",
- "j = len(str1)-1\n",
- "\n",
- "#Calculation\n",
- "while i < j:\n",
- " if str1[i] == str1[j]:\n",
- " test = 1\n",
- " else:\n",
- " test = 0\n",
- " break\n",
- " i += 1\n",
- " j -= 1\n",
- " \n",
- "#Result\n",
- "if test == 1:\n",
- " sys.stdout.write(\"Word is palindrome.\")\n",
- "else:\n",
- " sys.stdout.write(\"\\nWord is not Palindrome.\")"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter the word : ABBA\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Word is palindrome."
- ]
- }
- ],
- "prompt_number": 35
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.40, Page number: 271<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Compare the string using strcmp() function and display their ASCII difference.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "a1 = \"NAGPUR\"\n",
- "a2 = \"NAGPUR\"\n",
- "a3 = \"PANDHARPUR\"\n",
- "a4 = \"KOLHAPUR\"\n",
- "a5 = \"AURANGABAD\"\n",
- "a6 = \"HYDERABAD\"\n",
- "\n",
- "#Calculation\n",
- "#There is no built in function which returns the ascii value differece of two strings\n",
- "#ord function gives the ascii value of a character\n",
- "c1 = ord(a1[0]) - ord(a2[0])\n",
- "c2 = ord(a3[0]) - ord(a4[0])\n",
- "c3 = ord(a5[0]) - ord(a6[0])\n",
- "\n",
- "#Result\n",
- "sys.stdout.write(\"\\nAscii Difference between two strings\\n\")\n",
- "sys.stdout.write(\"Difference between (%s %s) = %d\\n\"%(a1,a2,c1))\n",
- "sys.stdout.write(\"Difference between (%s %s) = %d\\n\"%(a3,a4,c2))\n",
- "sys.stdout.write(\"Difference between (%s %s) = %d\\n\"%(a5,a6,c3))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Ascii Difference between two strings\n",
- "Difference between (NAGPUR NAGPUR) = 0\n",
- "Difference between (PANDHARPUR KOLHAPUR) = 5\n",
- "Difference between (AURANGABAD HYDERABAD) = -7\n"
- ]
- }
- ],
- "prompt_number": 53
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.41, Page number: 272<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Sort city names alphabetically.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "city = [['0' for i in range(5)]for i in range(20)]\n",
- "sys.stdout.write(\"Enter Names of Cities. \")\n",
- "for i in range(5):\n",
- " city[i] = raw_input(\"\")\n",
- " \n",
- "#Sorting & Result\n",
- "sys.stdout.write(\"Sorted List of Cities.\\n\\n\")\n",
- "for i in range(65,122+1):\n",
- " for j in range(0,5):\n",
- " if ord(city[j][0]) == i:\n",
- " sys.stdout.write(\"\\n%s\"%(city[j]))\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Names of Cities. "
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "MUMBAI\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "NANED\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "BANGLORE\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "KANPUR\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "INDORE\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Sorted List of Cities.\n",
- "\n",
- "\n",
- "BANGLORE\n",
- "INDORE\n",
- "KANPUR\n",
- "MUMBAI\n",
- "NANED"
- ]
- }
- ],
- "prompt_number": 37
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.42, Page number: 273<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Sort city names alphabetically\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "city = [['0' for i in range(5)]for i in range(20)]\n",
- "sys.stdout.write(\"Enter Names of Cities. \")\n",
- "for i in range(5):\n",
- " city[i] = raw_input(\"\")\n",
- " \n",
- "#Sorting & Result\n",
- "sys.stdout.write(\"Sorted List of Cities.\\n\\n\")\n",
- "for i in range(0,5):\n",
- " for j in range(0,5):\n",
- " if city[j-1] > city[j]:\n",
- " temp = city[j-1]\n",
- " city[j-1] = city[j]\n",
- " city[j] = temp\n",
- " \n",
- "for i in range(0,5):\n",
- " sys.stdout.write(\"\\n%s\"%(city[i]))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Names of Cities. "
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "MUMBAI\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "NANED\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "BANGLORE\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "KANPUR\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "INDORE\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Sorted List of Cities.\n",
- "\n",
- "\n",
- "BANGLORE\n",
- "INDORE\n",
- "KANPUR\n",
- "MUMBAI\n",
- "NANED"
- ]
- }
- ],
- "prompt_number": 38
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.43, Page number: 274<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Find number of words in a given statement.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "count = 1 #Counts the starting word\n",
- "i = 0\n",
- "text = raw_input(\"Enter The Line of Text\\nGive One Space After Each Word\")\n",
- "\n",
- "#Calculation\n",
- "while i < len(text): #There is no null terminating character in python\n",
- " if ord(text[i]) == 32:\n",
- " count += 1\n",
- " i += 1\n",
- " \n",
- "#Result\n",
- "sys.stdout.write(\"\\nThe Number of words in line = %d\"%(count))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter The Line of Text\n",
- "Give One Space After Each WordRead Books\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "The Number of words in line = 2"
- ]
- }
- ],
- "prompt_number": 40
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.44, Page number: 275<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Find number of words in a given statement.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "count = 0 \n",
- "i = 0\n",
- "text = raw_input(\"Enter Text Below :\")\n",
- "\n",
- "#Calculation\n",
- "while i < len(text): #There is no null terminating character in python for strings\n",
- " if ord(text[i]) >= 97 and ord(text[i]) <= 122 or ord(text[i]) >= 65 and ord(text[i]) <= 90:\n",
- " i += 1\n",
- " continue\n",
- " if ord(text[i]) == 32:\n",
- " count += 1\n",
- " i += 1\n",
- " continue\n",
- "\n",
- "if i == len(text):\n",
- " count += 1\n",
- " \n",
- "#Result\n",
- "sys.stdout.write(\"The Number of words in line = %d\"%(count))\n",
- " "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Text Below :Reading is a good Habit\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "The Number of words in line = 5"
- ]
- }
- ],
- "prompt_number": 41
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>Example 8.45, Page number: 276<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Sort the last name of mobile customers alphabetically.\n",
- "\n",
- "import sys\n",
- "\n",
- "#Variable Initialization\n",
- "fname = [['0' for i in range(20)] for i in range(10)]\n",
- "sname = [['0' for i in range(20)] for i in range(10)]\n",
- "surname = [['0' for i in range(20)] for i in range(10)]\n",
- "name = [['0' for i in range(20)] for i in range(20)]\n",
- "mobile = [['0' for i in range(20)] for i in range(10)]\n",
- "temp = ['0' for i in range(20)]\n",
- "\n",
- "sys.stdout.write(\"Enter First Name, Second Name, surname and Mobile Number \")\n",
- "for i in range(0,5):\n",
- " fname[i] = raw_input(\"\")\n",
- " sname[i] = raw_input(\"\")\n",
- " surname[i] = raw_input(\"\")\n",
- " mobile[i] = raw_input(\"\")\n",
- " \n",
- " name[i] = surname[i] + \" \" \n",
- " temp[0] = fname[i][0]\n",
- " name[i] = name[i] + temp[0] + \".\"\n",
- " temp[0] = sname[i][0]\n",
- " name[i] = name[i] + temp[0]\n",
- " \n",
- "for i in range(0,5):\n",
- " for j in range(1,5-i):\n",
- " if name[j-1] > name[j]:\n",
- " temp = name[j-1]\n",
- " name[j-1] = name[j]\n",
- " name[j] = temp\n",
- " temp = mobile[j-1]\n",
- " mobile[j-1] = mobile[j]\n",
- " mobile[j] = temp\n",
- " \n",
- "sys.stdout.write(\"List of Customers in alphanetical Order.\")\n",
- "for i in range(0,5):\n",
- " sys.stdout.write(\"\\n%-20s\\t %-10s\\n\"%(name[i],mobile[i]))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter First Name, Second Name, surname and Mobile Number "
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "K\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "S\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "MORE\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "458454\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "J\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "M\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "CHATE\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "658963\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "M\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "M\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "GORE\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "660585\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "L\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "K\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "JAIN\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "547855\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "J\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "J\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "JOSHI\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "354258\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "List of Customers in alphanetical Order.\n",
- "CHATE J.M \t 658963 \n",
- "\n",
- "GORE M.M \t 660585 \n",
- "\n",
- "JAIN L.K \t 547855 \n",
- "\n",
- "JOSHI J.J \t 354258 \n",
- "\n",
- "MORE K.S \t 458454 \n"
- ]
- }
- ],
- "prompt_number": 43
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [],
- "language": "python",
- "metadata": {},
- "outputs": []
- }
- ],
- "metadata": {}
- }
- ]
-}
\ No newline at end of file |