> For the complete documentation index, see [llms.txt](https://scls-cs.gitbook.io/scls-apcs-lab/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://scls-cs.gitbook.io/scls-apcs-lab/methods/lab3-string-magic.md).

# Lab3: String Magic

In this assignment, you will create a class called "StringMagic":

### Step1

Create a class called StringMagic:

### Step2

Create a method called **`String removeSpace(String str)`**, which is to remove the space of *str* and return a new String. <mark style="background-color:red;">**Assume str contains only one space.**</mark>  For example, if you call the method in main(), the output will look like this:

<pre class="language-java"><code class="lang-java"><strong>StringMagic obj = new StringMagic();
</strong>
System.out.println(obj.removeSpace("Hello World"));//will print "HelloWorld"

System.out.println(obj. removeSpace("Java Rocks"));//will print "JavaRocks"
</code></pre>

### Step3

Create a method called **`String replaceSpace(String str)`**, which is to replace the space of str with three dots. <mark style="background-color:red;">**Assume str contains only two spaces.**</mark> For example:&#x20;

```java
StringMagic obj = new StringMagic();

System.out.println(obj.replaceSpace("Welcome to SCLS"));//will print "Welcome...to...SCLS"

System.out.println(obj.replaceSpace("I am fine"));//will print "I...am...fine"
```

### Step4

You can think of String as a sequence of characters. **The char data type** is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c'. For example, "Hello" is a String, which consists of 5 characters: 'H', 'e', 'l', 'l', and 'o'.

We can use **str.charAt(int index)** to get the character at the specified index in a string. For example:

```java
String x = "xyz";
char ch = x.charAt(0);
System.out.println(ch); //will print 'x'
```

In Java, a character can be converted to an integer, which is known as **`ASCII code`**, using type casing method. For example:

```java
char character = 'a';    
int ascii = (int) character; //convert char to int
System.out.println(ascii);   //will print 97
```

You can do some tricks to manipulate Strings and characters, like ***convert integer to character***, or ***concatenate Strings with characters***. For example:

```java
public class Main
{
    public static void main(String[] args) {
        String s = "xyz";
        char ch = s.charAt(0);
        int ascii = (int)ch+1;
        ch = (char)ascii;    //convert integer to character
        System.out.println(ch);
        s = s + ch;          //concatenate strings with char
        System.out.println(s);
    }
}
```

Please copy and paste the program in Step4, run the program, and observe the results. It is an important pre-task to finish Step5. You don't have to submit anything in this step. You can remove the code after running it.

### Step5

In cryptography, a Caesar cipher is one of the simplest and most widely known encryption techniques. <mark style="color:orange;">For example, with a shift of 3, A would be replaced by D, E would become H, and so on</mark>. The method is named after Julius Caesar, who used it in his private correspondence.

You will write a method **`String cipher(String str, int shift)`** , which is to encrypt str to a new String. **Assume str only contains 3 English letters,&#x20;**~~**both uppercase and lowercase is allowed**~~**.** For example:

```java
StringMagic obj = new StringMagic();

System.out.println(obj.cipher("red",1));//will print "sfe"
```

An important note: if a char shift has exceeded 26 characters, then it will rotate back to the beginning of the alphabet. For example:&#x20;

```java
StringMagic obj = new StringMagic();

System.out.println(obj.cipher("xyz",2));//will print "zab"
```

### Submit:

Share your project link to 钉钉作业本 by Oct 8th, 22:00PM.

&#x20;


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://scls-cs.gitbook.io/scls-apcs-lab/methods/lab3-string-magic.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
