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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
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": {}
}
]
}
|