summaryrefslogtreecommitdiff
path: root/A_First_course_in_Programming_with_C/Chapter8.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'A_First_course_in_Programming_with_C/Chapter8.ipynb')
-rwxr-xr-xA_First_course_in_Programming_with_C/Chapter8.ipynb665
1 files changed, 665 insertions, 0 deletions
diff --git a/A_First_course_in_Programming_with_C/Chapter8.ipynb b/A_First_course_in_Programming_with_C/Chapter8.ipynb
new file mode 100755
index 00000000..2121f615
--- /dev/null
+++ b/A_First_course_in_Programming_with_C/Chapter8.ipynb
@@ -0,0 +1,665 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 8: String Manipulations in C<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 1, Page number: SMC-4<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Count a character in a string\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "st = raw_input(\"Enter the string : \")\n",
+ "ch = raw_input(\"Which char to be counted ?\")\n",
+ "\n",
+ "#Checking\n",
+ "l = len(st)\n",
+ "count = 0\n",
+ "for i in st:\n",
+ " if i == ch:\n",
+ " count = count + 1\n",
+ " \n",
+ "#Output\n",
+ "sys.stdout.write(\"\\nThe character %c occurs %d times\"%(ch,count))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the string : MISSISSIPPI\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Which char to be counted ?S\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The character S occurs 4 times"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 2, Page number: SMC-5<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Counting vowels\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "st = raw_input(\"Enter the sentence :\")\n",
+ "count = 0\n",
+ "\n",
+ "#Processing\n",
+ "for i in st:\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 = count + 1\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"%d vowels are present in the sentence\"%(count))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the sentence :This is a book\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5 vowels are present in the sentence"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 3, Page number: SMC-6<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Palindrome or not \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "rst = ['' for i in range(0,20)]\n",
+ "st = raw_input(\"Enter the string : \")\n",
+ "j = len(st)-1\n",
+ "\n",
+ "#Processing\n",
+ "rst = st[::-1]\n",
+ "\n",
+ "#Result\n",
+ "if st == rst:\n",
+ " sys.stdout.write(\"%s is a palindrome string\"%(st))\n",
+ "else:\n",
+ " sys.stdout.write(\"%s is not a palindrome string\"%(st))\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the string : HYDERABAD\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "HYDERABAD is not a palindrome string"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4, Page number: SMC-8<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#String Concatenation\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "st1 = raw_input(\"Enter first string : \")\n",
+ "st2 = raw_input(\"Enter second string : \")\n",
+ "\n",
+ "#Processing\n",
+ "st = st1 + \" \" + st2\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nResultant string is %s\"%(st))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first string : NEW\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second string : DELHI\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Resultant string is NEW DELHI"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 5, Page number: SMC-10<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Print greater string\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "st1 = raw_input(\"Enter string 1 : \")\n",
+ "st2 = raw_input(\"Enter string 2 : \")\n",
+ "\n",
+ "\n",
+ "#Result\n",
+ "if st1 > st2:\n",
+ " sys.stdout.write(\"%s is alphabetically greater string\"%(st1))\n",
+ "else:\n",
+ " sys.stdout.write(\"%s is alphabetically greater string\"%(st2))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter string 1 : ALPHA\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter string 2 : BETA\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "BETA is alphabetically greater string"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 6, Page number: SMC-11<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#String sorting\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "names = [\"\" for i in range(0,50)]\n",
+ "n = int(raw_input(\"How many names ? \"))\n",
+ "sys.stdout.write(\"\\nEnter the %d names one by one\"%(n))\n",
+ "\n",
+ "for i in range(0,n):\n",
+ " names[i] = raw_input(\"\")\n",
+ " \n",
+ "for i in range(0,n):\n",
+ " for j in range(i+1,n):\n",
+ " if names[i] > names[j]:\n",
+ " temp = names[i]\n",
+ " names[i] = names[j]\n",
+ " names[j] = temp\n",
+ " \n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nNames in alphabetical order\")\n",
+ "for i in range(0,n):\n",
+ " sys.stdout.write(\"\\n%s\"%(names[i]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many names ? 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter the 4 names one by one"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "DEEPAK\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "SHERIN\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "SONIKA\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "ARUN\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Names in alphabetical order\n",
+ "ARUN\n",
+ "DEEPAK\n",
+ "SHERIN\n",
+ "SONIKA"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 7, Page number: SMC-13<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Lower case text to upper case\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "st = raw_input(\"Enter a sentence : \")\n",
+ "\n",
+ "#Result\n",
+ "st = st.upper()\n",
+ "\n",
+ "sys.stdout.write(\"\\nThe converted upper case string is \\n%s\"%(st))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a sentence : logical thinking is a must to learn programming\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The converted upper case string is \n",
+ "LOGICAL THINKING IS A MUST TO LEARN PROGRAMMING"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 8, Page number: SMC-14<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Determine the longest word\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "\n",
+ "#Variable Initialization\n",
+ "st = raw_input(\"Enter a sentence \")\n",
+ "\n",
+ "#Processing\n",
+ "lngst = max(st.split(), key=len)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nLongest word is %s\"%(lngst))\n",
+ "sys.stdout.write(\"\\nPress any key to continue...\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a sentence SMALL THINGS MAKE PERFECTION BUT PERFECTION IS NO SMALL THING\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Longest word is PERFECTION\n",
+ "Press any key to continue..."
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 9, Page number: SMC-16<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Remove particular word in a text\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "st = raw_input(\"Enter a sentence : \")\n",
+ "\n",
+ "omit = raw_input(\"\\nEnter word to omit : \")\n",
+ "word = st.split()\n",
+ "newst = ' '.join([i for i in word if i not in omit])\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nAfter omitting the word %s\\n%s\"%(omit,newst))\n",
+ "sys.stdout.write(\"\\nPress any key to continue...\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a sentence : TO ACCESS THE NAME OF THE CITY IN THE LIST\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter word to omit : THE\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "After omitting the word THE\n",
+ "TO ACCESS NAME OF CITY IN LIST\n",
+ "Press any key to continue..."
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 10, Page number: SMC-17<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Telegram expense calculation\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "st = raw_input(\"Type the sentence for Telegram : \")\n",
+ "\n",
+ "count = len(st.split())\n",
+ "\n",
+ "if count <= 10:\n",
+ " amt = 5\n",
+ "else:\n",
+ " amt = 5 + (count - 10) * 1.25\n",
+ " \n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nAmount to be paid for telegram = Rs. %6.2f\"%(amt))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Type the sentence for Telegram : Congratulations on your success in Examinations.\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Amount to be paid for telegram = Rs. 5.00"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 11, Page number: SMC-19<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Count lines, words and characters\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "txt = raw_input(\"Enter the text, type $st end\\n\\n\")\n",
+ "\n",
+ "wds = 0\n",
+ "lns = 0\n",
+ "chs = 0\n",
+ "\n",
+ "#Processing\n",
+ "for i in txt:\n",
+ " if i == ',' or i == '!' or i == '\\t' or i == ' ':\n",
+ " wds += 1\n",
+ " chs += 1\n",
+ " else:\n",
+ " if i == '?' :\n",
+ " \n",
+ " lns += 1\n",
+ " else:\n",
+ " if i == '.':\n",
+ " wds += 1\n",
+ " lns += 1\n",
+ " else:\n",
+ " chs += 1\n",
+ " \n",
+ "#Result\n",
+ "sys.stdout.write(\"\\n\\nNumber of char(incl.blanks) = %d\"%(chs))\n",
+ "sys.stdout.write(\"\\nNumber of words = %d\"%(wds))\n",
+ "sys.stdout.write(\"\\nNumber of lines = %d\"%(lns))\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the text, type $st end\n",
+ "\n",
+ "What is a string? How do you initialize it? Explain with example.$\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "Number of char(incl.blanks) = 63\n",
+ "Number of words = 12\n",
+ "Number of lines = 3"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file