数据结构实验四 串操作 下载本文

内容发布更新时间 : 2024/5/5 8:19:13星期一 下面是文章的全部内容请认真阅读。

实验报告四 串操作

班级: 姓名: 学号: 专业:

一、 实验目的:

(1) 掌握串的定义、术语。 (2) 掌握串的基本操作算法。 (3) 掌握串的匹配算法。 二、 实验内容:

1. 在常量串MyString类中,增加以下方法,并求各算法的时间复杂度。 public MyString trim() //删除串中所有空格 public char[] toCharArray() //返回字符数组

public MyString toLowerCase() //返回将大写字母转换成小写字母的字符串 public MyString toUpperCase() //返回将小写字母转换成大写字母的字符串

public MyString replace(char old, char newc) //用字符newc替换串中的字符old public Boolean equals(Object obj) //判断两个串是否相等

public Boolean equalsIgnoreCase(String1 str) //判断两个串是否相等,忽略大小写 public int compareTo(MyString str) //比较两个串大小,实现Comparable接口 public int compareToIgnoreCase(MyString str) //比较两个串大小,忽略大小写 public Boolean startsWith(MyString prefix) //判断prefix是否前缀子串 public Boolean endsWith(MyString suffix) //判断suffix是否前缀子串 源代码:

package Word1;

public class MyString {

char[] str;

public MyString trim() //删除串中所有空格 { }

public char[] toCharArray() { }

public MyString toLowerCase() //返回将大写字母转换成小写字母的字符串 {

for(int i=0;i

if(str[i]>=65 && str[i]<=90 ){

str[i]=(char) (str[i]+32);

return str;

//返回字符数组

char[] n=new char[str.length]; int cou=0;

for(int i=0;i

if(str[i]!=' '){ }

n[cou]=str[i]; cou++;

}

}

}

return this;

public MyString toUpperCase() //返回将小写字母转换成大写字母的字符串 { }

public MyString replace(char old, char newc) //用字符newc替换串中的字符{ }

public boolean equals(Object obj) //判断两个串是否相等 { }

public boolean equalsIgnoreCase(String str1)//判断两个串是否相等,忽略大小写 {

char[] c=new char[str1.length()];

if(obj instanceof String){ }

return true;

String s=obj.toString(); char[] c=new char[s.length()]; for(int i=0;i

c[i]=s.charAt(i); return false;

for(int i=0;i

if(c[i]!=str[i])

return false;

if(c.length!=str.length) else{

for(int i=0;i

return this;

if(str[i]==old)

str[i]=newc;

for(int i=0;i

return this;

if(str[i]>=97 && str[i]<=122 ){ }

str[i]=(char) (str[i]-32);

old

}

for(int i=0;i

return true;

c[i]=str1.charAt(i); return false;

for(int i=0;i

if(str[i]>=65 && str[i]<=90 ){ } else{ }

if(c[i]!=str[i] && c[i]!=str[i]-32)

return false;

if(c[i]!=str[i] && c[i]!=str[i]+32)

return false;

if(c.length!=str.length) else{

public int compareTo(MyString str) //比较两个串大小,实现Comparable接口 {

int c;

if(this.str.length==str.str.length) } else{

c=this.str.length; for(int i=0;i

if(this.str[i]==str.str[i])

continue;

if(this.str[i]>str.str[i]) c=str.str.length; c=str.str.length; for(int i=0;i

return 0;

if(this.str[i]==str.str[i]) }

continue; return 0; return 1;

if(this.str[i]>str.str[i]) else{

if(this.str.length>str.str.length){

}

}

}

}

return 0; return 1;

else{

return 1;

public int compareToIgnoreCase(MyString str) //比较两个串大小,忽略大小写 { }

int c;

char[] c1=new char[this.str.length]; char[] c2=new char[str.str.length]; c1=this.toUpperCase().str; c2=str.toUpperCase().str;

if(this.str.length==str.str.length) } else{ }

return 1;

c=this.str.length; for(int i=0;i

if(c1[i]==c2[i]) }

continue; return 0; return 1; if(c1[i]>c2[i]) else{

c=str.str.length; c=str.str.length; for(int i=0;i

return 0;

if(c1[i]==c2[i]) }

continue; return 0; return 1; if(c1[i]>c2[i]) else{

if(this.str.length>str.str.length){

}

public boolean startsWith(MyString prefix) //判断prefix是否前缀子串 { }

public boolean endsWith(MyString suffix) //判断suffix是否前缀子串 { }

char[] c=new char[suffix.str.length];

for(int i=this.str.length,j=c.length;j>=0;i--,j--){ }

return true;

if(c[j]==this.str[i])

continue; return false; else

char[] c=new char[prefix.str.length]; for(int i=0;i

return true;

if(c[i]==this.str[i])

continue; return false; else

2. 统计在一个串中各字符的出现次数。

源代码:package Word1;

public class MyString {

char[] str;

public MyString trim() //删除串中所有空格 {

char[] n=new char[str.length]; int cou=0;

for(int i=0;i

if(str[i]!=' '){ }

n[cou]=str[i]; cou++;