summaryrefslogtreecommitdiff
path: root/_Programming_With_C/chapter10.ipynb
blob: 083baffa8f04e70cd2c79e84b07762beed8f2ea8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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": {}
  }
 ]
}