-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathREADME.txt
More file actions
681 lines (378 loc) · 20.2 KB
/
README.txt
File metadata and controls
681 lines (378 loc) · 20.2 KB
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
70 Java String Exercises.
How many of these can you do in two 1-hour sessions?
If at first you cannot see the answer quickly, move to the next problem.
Implement the tests suggested by the Sample Output and write the method that makes the tests succeed.
1. Write a Java program to get the character at the given index within the String.
Sample Output:
Original String = Java Exercises!
The character at position 0 is J
The character at position 10 is i
2. Write a Java program to get the character (Unicode code point) at the given index (1 and 9) within the String.
Sample Output:
Original String : w3resource.com
Character(unicode point) = 51
Character(unicode point) = 101
3. Write a Java program to get the character (Unicode code point) BEFORE the specified index (1 and 9) within the String.
Sample Output:
Original String : w3resource.com
Character(unicode point) = 119
Character(unicode point) = 99
4. Write a java program to count a number of Unicode code points in the specified text range of a String.
Sample Output:
Original String : w3rsource.com
Codepoint count = 9
5. Write a java program to compare two strings lexicographically.
Sample Output:
String 1: This is Exercise 1
String 2: This is Exercise 2
"This is Exercise 1" is less than "This is Exercise 2"
6. Write a java program to compare two strings lexicographically, ignoring case differences.
Sample Output:
String 1: This is exercise 1
String 2: This is Exercise 1
"This is exercise 1" is equal to "This is Exercise 1"
7. Write a Java program to concatenate a given string to the end of another string.
Sample Output:
String 1: PHP Exercises and
String 2: Python Exercises
The concatenated string: PHP Exercises and Python Exercises
8. Write a Java program to test if a given string contains the specified sequence of char values.
Sample Output:
Original String: PHP Exercises and Python Exercises
Specified sequence of char values: and
true
9. Write a Java program to compare a given string to the specified character sequence.
Sample Output:
Comparing example.com and example.com: true
Comparing Example.com and example.com: false
10. Write a Java program to compare a given string to the specified string buffer.
Sample Output:
Comparing example.com and example.com: true
Comparing Example.com and example.com: false
11. Write a Java program to create a new String object with the contents of a character array.
Sample Output:
The book contains 234 pages.
12. Write a Java program to check whether a given string ends with the contents of another string.
Sample Output:
"Python Exercises" ends with "se"? false
"Python Exercise" ends with "se"? true
13. Write a Java program to check whether two String objects contain the same data.
Sample Output:
"Stephen Edwin King" equals "Walter Winchell"? false
"Stephen Edwin King" equals "Mike Royko"? false
14. Write a Java program to compare a given string to another string, ignoring case considerations.
Sample Output:
"Stephen Edwin King" equals "Walter Winchell"? false
"Stephen Edwin King" equals "stephen edwin king"? true
15. Write a java program to print current date and time in the specified format.
Sample Output:
Current Date and Time :
June 19, 2017
3:13 pm
N.B. : The current date and time will change according to your system date and time.
16. Write a Java program to get the contents of a given string as a byte array.
Sample Output:
The new String equals This is a sample String.
17. Write a Java program to get the contents of a given string as a character array.
Sample Output:
The char array equals "[C@2a139a55"
18. Write a Java program to create a unique identifier of a given string.
Sample Output:
The hash for Python Exercises. is 863132599
19. Write a Java program to get the index of all the characters of the alphabet.
Sample Output:
a b c d e f g h i j
=========================
36 10 7 40 2 16 42 1 6 20
k l m n o p q r s t
===========================
8 35 22 14 12 23 4 11 24 31
u v w x y z
================
5 27 13 18 38 37
Sample string of all alphabet: "The quick brown fox jumps over the lazy dog."
20. Write a Java program to get the canonical representation of the string object.
Sample Output:
str1 == str2? false
str1 == str3? true
21. Write a Java program to get the last index of a string within a string.
Sample Output:
a b c d e f g h i j
===========================
36 10 7 40 33 16 42 32 6 20
k l m n o p q r s t
===========================
8 35 22 14 41 23 4 29 24 31
u v w x y z
=================
21 27 13 18 38 37
Sample string of all alphabet: "The quick brown fox jumps over the lazy dog."
22. Write a java program to get the length of a given string.
Sample Output:
The string length of 'example.com' is: 11
23. Write a Java program to find whether a region in the current string matches a region in another string.
Sample Output:
str1[0 - 7] == str2[28 - 35]? true
str1[9 - 15] == str2[9 - 15]? false
24. Write a Java program to replace all the 'd' characters with 'f' characters.
Sample Output:
Original string: The quick brown fox jumps over the lazy dog.
New String: The quick brown fox jumps over the lazy fog.
25. Write a Java program to replace each substring of a given string that matches the given regular expression with the given replacement.
Sample string : "The quick brown fox jumps over the lazy dog."
In the above string replace all the fox with cat.
Sample Output:
Original string: The quick brown fox jumps over the lazy dog.
New String: The quick brown cat jumps over the lazy dog.
26. Write a Java program to check whether a given string starts with the contents of another string.
Sample Output:
Red is favorite color. starts with Red? true
Orange is also my favorite color. starts with Red? false
27. Write a Java program to get a substring of a given string between two specified positions.
Sample Output:
old = The quick brown fox jumps over the lazy dog.
new = brown fox jumps
28. Write a Java program to create a character array containing the contents of a string.
Sample Output:
Java Exercises.
29. Write a Java program to convert all the characters in a string to lowercase.
Sample Output:
Original String: The Quick BroWn FoX!
String in lowercase: the quick brown fox!
30. Write a Java program to convert all the characters in a string to uppercase.
Sample Output:
Original String: The Quick BroWn FoX!
String in uppercase: THE QUICK BROWN FOX!
31. Write a Java program to trim any leading or trailing whitespace from a given string.
Sample Output:
Original String: Java Exercises
New String: Java Exercises
32. Write a Java program to find longest Palindromic Substring within a string.
Sample Output:
The given string is: thequickbrownfoxxofnworbquickthe
The longest palindrome substring in the giv
en string is; brownfoxxofnworb
The length of the palindromic substring is: 16
33. Write a Java program to find all interleavings of given strings.
Sample Output:
The given strings are: WX YZ
The interleavings strings are:
YWZX
WYZX
YWXZ
WXYZ
YZWX
WYXZ
34. Write a Java program to find the second most frequent character in a given string.
Sample Output:
The given string is: successes
The second most frequent char in the string is: c
35. Write a Java program to print all permutations of a given string with repetition.
Sample Output:
The given string is: PQR
The permuted strings are:
PPP
PPQ
PPR
...
RRP
RRQ
RRR
36. Write a Java program to check whether two strings are interliving of a given string. Assuming that the unique characters in both strings.
Sample Output:
The given string is: PMQNO
The interleaving strings are MNO and PQ
The given string is interleaving: true
The given string is: PNQMO
The interleaving strings are MNO and PQ
The given string is interleaving: false
37. Write a Java program to find Length of the longest substring without repeating characters.
Sample Output:
Input String : pickoutthelongestsubstring
The longest substring : [u, b, s, t, r, i, n, g]
The longest Substring Length : 8
38. Write a Java program to print after removing duplicates from a given string.
Sample Output:
The given string is: w3resource
After removing duplicates characters the new string is: w3resouc
39. Write a Java program to find first non repeating character in a string.
Sample Output:
The given string is: gibblegabbler
The first non repeated character in String is: i
40. Write a Java program to divide a string in n equal parts.
Sample Output:
The given string is: abcdefghijklmnopqrstuvwxy
The string divided into 5 parts and they are:
abcde
fghij
klmno
pqrst
uvwxy
41. Write a Java program to remove duplicate characters from a given string presents in another given string.
Sample Output:
The given string is: the quick brown fox
The given mask string is: queen
The new string is:
th ick brow fox
42. Write a Java program to print list items containing all characters of a given word.
Sample Output:
The given strings are: rabbit bribe dog
The given word is: bib
The strings containing all the letters of the given word are:
rabbit
bribe
43. Write a Java program to find the maximum occurring character in a string.
Sample Output:
The given string is: test string
Max occurring character in the given string is: t
44. Write a Java program to reverse a string using recursion.
Sample Output:
The given string is: The quick brown fox jumps
The string in reverse order is:
spmuj xof nworb kciuq ehT
45. Write a Java program to reverse words in a given string.
Sample Output:
The given string is: Reverse words in a given string
The new string after reversed the words: string given a in words Reverse
46. Write a Java program to reverse every word in a string using methods.
Sample Output:
The given string is: This is a test string
The string reversed word by word is:
sihT si a tset gnirts
47. Write a Java program to rearrange a string so that all same characters become d distance away.
Sample Output:
The given string is: accessories
The string after arrange newly is:
secrsecisao
48. Write a Java program to remove "b" and "ac" from a given string.
Sample Output:
The given string is: abrambabasc
After removing the new string is: aramaasc
49. Write a Java program to find first non-repeating character from a stream of characters.
Sample Output:
String: godisgood
Reading: g
The first non-repeating character so far is: g
Reading: o
The first non-repeating character so far is: g
Reading: d
The first non-repeating character so far is: g
Reading: i
The first non-repeating character so far is: g
Reading: s
The first non-repeating character so far is: g
Reading: g
The first non-repeating character so far is: o
Reading: o
The first non-repeating character so far is: d
Reading: o
The first non-repeating character so far is: d
Reading: d
The first non-repeating character so far is: i
50. Write a Java program to find lexicographic rank of a given string.
Sample Output:
The Given String is: BDCA
The Lexicographic rank of the given string is: 12
N.B.: Total possible permutations of BDCA are(lexicographic order) :
ABCD ABDC ACBD ACDB ADBC ADCB BACD BADC BCAD BCDA BDAC BDCA
1 2 3 4 5 6 7 8 9 10 11 12
The BDCA appear in 12 position of permutation (lexicographic order).
51. Write a Java program to count and print all the duplicates in the input string.
Sample Output:
The given string is: w3resource
The duplicate characters and counts are:
e appears 2 times
r appears 2 times
52. Write a Java program to check if two given strings are rotations of each other.
Sample Output:
The given strings are: ABACD and CDABA
The concatination of 1st string twice is: ABACDABACD
The 2nd string CDABA exists in the new string.
Strings are rotations of each other
53. Write a Java program to match two strings where one string contains wildcard characters.
Sample Output:
The given string is: abcdhgh
The given pattern string is: abc*d?*
The given pattern is matching.
54. Write a Java program to find the smallest window in a string containing all characters of another string.
Sample Output:
The given string is: welcome to w3resource
Characters to find in the main sring are: tower
The smallest window which contains the finding characters is : to w3re
55. Write a Java program to remove all adjacent duplicates recursively from a given string.
Sample Output:
The given string is: aabaarbarccrabmq
The new string after removing all adjacent duplicates is:
brmq
56. Write a Java program to append two given strings such that, if the concatenation creates a double characters then omit one of the characters.
Sample Output:
The given strings are: food and door
The string after concatination are: foodoor
57. Write a Java program to return a new string where the last two characters of a given string, if present, are swapped.
Sample Output:
The given strings is: string
The string after swap last two characters are: strign
58. Write a Java program to read a string and return true if it ends in "ng".
Sample Output:
The given strings is: string
The string containing ng at last: true
The given strings is: strign
The string containing ng at last: false
59. Write a Java program to read a string,if the string begins with "red" or "black" return that color string, otherwise return the empty string.
Sample Output:
The given strings is: blacksea
The string begins with the color: black
60. Write a Java program to read two strings append them together and return the result. If the strings are different lengths, omit chars from the beginning of longer string and make them equal length.
Sample Output:
The given strings is: Welcome and home
The new string is: comehome
61. Write a Java program to read a string and an int n, return a string made of the first and last n characters from the string.
Sample Output:
The given strings is: Welcome
The given numbers is: 3
The new string is: Welome
62. Write a Java program to read a string and return true if "good" appears starting at index 0 or 1 in the given string.
Sample Output:
The given strings is: goodboy
The 'good' appear in the string is: true
63. Write a Java program to return true from a given string if the first two characters in the string also appear at the end.
Sample Output:
The given strings is: educated
The first two characters appear in the last is: true
64. Write a Java program to read a string and if a substring of length two appears at both its beginning and end, return a string without the substring at the beginning otherwise, return the original string unchanged.
Sample Output:
The given strings is: educated
The new string is: ucated
65. Write a Java program to read a string and if the first or last characters are 't', return the string without those 't' otherwise return the string unchanged.
Sample Output:
The given strings is: testcricket
The new string is: estcricke
66. Write a Java program to read a string and return the string without the first two characters.Except keep the first char if it is 'g' and keep the second char if it is 'h'.
Sample Output:
The given strings is: goat
The new string is: gat
he given strings is: photo
The new string is: hoto
The given strings is: ghost
The new string is: ghost
67. Write a Java program to read a string and if one or both of the first tow characters is 'x', return without those 'x',otherwise return the string unchanged.
Sample Output:
The given strings is: oocyte
The new string is: cyte
The given strings is: boat
The new string is: bat
The given strings is: own
The new string is: wn
68. Write a Java program to read a string and returns after remove the # and its immediate left and right characters.
Sample Output:
The given strings is: test#string
The new string is: testring
The given strings is: test##string
The new string is: testring
The given strings is: test#the#string
The new string is: teshtring
69. Write a Java program to return the substring that is between the first and last appearance of the substring 'toast' in the given string,or return the empty string if substirng 'toast' does not exists.
Sample Output:
The given strings is: sweettoastbuttertoast
The new string is: butter
from a page by www.w3resource.com