Beruflich Dokumente
Kultur Dokumente
String Operations
1. String
generally implemented as an array of bytes (or words) which stores a series of characters,
using some character encoding. Characters canhave one of 216 possible values.
String = Hello
Pseudo code:
main()
| String s = Monika
Character = n
1|Page
3. Length
int length();
- The length of a string is computed by counting the quantity of characters in a String; this
Pseudo code:
main()
| String s = Monika
Length = 6
4. Concatenation
2|Page
Pseudo code:
String a;
String concat(String b)
| return a + b
Pseudo code:
main()
| String s1 = Monika
| String s2 = Agarwal
- Output:
Concat = MonikaAgarwal
5. Lowercase
String toLowerCase();
Pseudo code:
main()
3|Page
| String s1 = MONIKA
| String s2 = s1.toLowerCase()
- Output:
6. Substring
- This method returns a new String which is a substring of the current String. The substring
begins with the character at the first given specified index a and extends to thesecond given
index.
Pseudo code:
main()
| String s = MONIKA
Example = ONI
4|Page
7. Trim
String trim();
- This method returns a copy of the string withoutleading and trailing whitespace.
Pseudo code:
main()
| String s = Hi I am Monika
5|Page
II. String Sort assignment
Write an algorithm which will arrange the following numbers (in string representations) in
ascending order: 158, 124, 238, 707, 608, 250, 888 (using LSD or MSD
approaches).
The best solution for this assignment is choosing LSD String Sort,
This assignment will be using LSD String Sort, which is known as least-significant-digit
string sort. LSD is a sorting method based on key-indexed counting. If the strings all haveits
length N, the method willsort the strings N times with key-indexed counting, using each of
the positionsas the key, proceeding from right to left. The main important point LSD methods
is only applied when the length of all String are the same.
Assignment
The function of LSD is easy to understand if you fully understand key-indexing counting:
First of all, the method declared R as 216=256total values of every character in ASCII Code
6|Page
It can be clearly seen that the method is mostly built based on the code of key-
indexed counting. The only difference between those two methods is that LSD will loop the
sorting process to sort each character in the Strings from right to left. So the algorithm below
is essential in the method which d is the index position in the Strings having N length -
starting at W-1 (W is a total number of character needed to be sort that user will put in), it
will minus 1 after each dth character is sorted until the index reaches 0:
+ Algorithms inside the loop is exactly the key-indexed counting algorithms (Read
more in Section II.1 tounderstand the process) and the method use charAt() in Javas String
class provided to find a specified characterfrom the strings by its index position d. The
process will sort character by characterof Strings by key-index counting from right to left
until d = 0. Finally, the method with copy the aux[] array to given String[ ] array by
7|Page
+ The main() function :
- So applying to a collection of String in the assignment, let see what the method will return:
- It works perfectly fine. The array of String has been arranged neatly.
8|Page
Test case:
The natural way to implement this idea is a recursive method known as most-significant-
digit-first (MSD) string sort. We use key-indexed counting to sort the strings according to
their first character, then (recursively) sort the subarrays corresponding to each character
(excluding the first character, which we know to be the same for each string in each
subarray).
9|Page
-LSD is faster than MSD when there is a fixed length. MSD is too slow for small String, and
- But, MSD is more popularly applied because of its mobility of sorting keys in lexicographic
order, and LSD only works when the lengths of all String are the same.
10 | P a g e
11 | P a g e