实验报告?/p>
串操?/p>
班级?/p>
姓名?/p>
学号?/p>
专业?/p>
一?/p>
实验目的?/p>
?/p>
1
?/p>
掌握串的定义、术语?/p>
?/p>
2
?/p>
掌握串的基本操作算法?/p>
?/p>
3
?/p>
掌握串的匹配算法?/p>
二?/p>
实验内容?/p>
1.
在常量串
MyString
类中,增加以下方法,并求各算法的时间复杂度?/p>
public MyString trim()
//
删除串中所有空?/p>
public char[] toCharArray()
//
返回字符数组
public MyString toLowerCase()
//
返回将大写字母转换成小写字母的字符串
public MyString toUpperCase()
//
返回将小写字母转换成大写字母的字符串
public MyString replace(char old, char newc)
//
用字?/p>
newc
替换串中的字?/p>
old
public Boolean equals(Object obj)
//
判断两个串是否相?/p>
public Boolean equalsIgnoreCase(String1 str) //
判断两个串是否相?/p>
,
忽略大小?/p>
public int compareTo(MyString str)
//
比较两个串大小,实现
Comparable
接口
public int compareToIgnoreCase(MyString str)
//
比较两个串大小,忽略大小?/p>
public Boolean startsWith(MyString prefix)
//
判断
prefix
是否前缀子串
public Boolean endsWith(MyString suffix)
//
判断
suffix
是否前缀子串
源代码:
package
Word1;
public
class
MyString {
char
[]
str
;
public
MyString trim()
//
删除串中所有空?/p>
{
char
[] n=
new
char
[
str
.
length
];
int
cou=0;
for
(
int
i=0;i<
str
.
length
;i++){
if
(
str
[i]!=
' '
){
n[cou]=
str
[i];
cou++;
}
}
str
=n;
return
this
;
}
public
char
[] toCharArray()
//
返回字符数组
{
return
str
;
}
public
MyString toLowerCase()
//
返回将大写字母转换成小写字母的字符串
{
for
(
int
i=0;i<
str
.
length
;i++){
if
(
str
[i]>=65 &&
str
[i]<=90 ){
str
[i]=(
char
) (
str
[i]+32);