Sie sind auf Seite 1von 11

I.

String Operations

1. String

- A String is defined as a sequence of letters.A string is oftenknown as a data type and is

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

2. Find Character (Indexing)

String charAt(int a);

- We can understand this operation as a method finding a specified characterfrom a stringby

its index position a in array.

Pseudo code:

main()

| String s = Monika

| print Character = + s.charAt(2)

- After executing, it prints the following result:

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

method returns the length of String.

- Here is a pseudo code of operation of charAt() method in String.:

Pseudo code:

main()

| String s = Monika

| print Length = + s.length()

- After executing, it prints the following result:

Length = 6

4. Concatenation

String concate(String a);

- Concatenation method is used to join two or more strings.

- Here is a pseudo code of operation of concat() method in String.:

2|Page
Pseudo code:

String a;

String concat(String b)

| return a + b

Pseudo code:

main()

| String s1 = Monika

| String s2 = Agarwal

| print Concat = + s3 = s1.concat(s2)

- Output:

Concat = MonikaAgarwal

5. Lowercase

String toLowerCase();

- This method converts a string to lowercase letters.

We have a pseudo code as an example of function:

Pseudo code:

main()

3|Page
| String s1 = MONIKA

| String s2 = s1.toLowerCase()

| print Lower Case = + s2

- Output:

Lower Case = monika

6. Substring

String substring(int a, int b);

- 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.

- We have a pseudo code as an example of function:

Pseudo code:

main()

| String s = MONIKA

| print Example = +s.substring(1,3)

- After executing, it prints the following result:

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

| print Trim = + s.trim() + .

- After executing, it prints the following result:

Trim =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,

LSDString Sort and assignment

A. Definition of 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

and N as a length of the String:

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

arraycopy() function borrowed in Java.

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:

Test ID LSD String Sort

- String[] a = {"158", "124", "238", "707", "608", "250", "888"};


Input steps
- sort(a,3)

Output Array a is sorted; process terminates

Actual Result PASS

Test ID LSD String Sort

- String[] a = {"158", "124", "238", "707", "608", "250", "888"};


Input steps
- sort(a,4)

Output Throw StringIndexOutOfBoundsException; process terminates

Actual Result FAIL

Comparison with MSD and Judgement

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

it need huge number of recursive calls on small String.

- 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

Das könnte Ihnen auch gefallen