For this programming assignment I have to create my own verision of class string
ID: 3718110 • Letter: F
Question
For this programming assignment I have to create my own verision of class string. These are the methods I need to do
MyString( String original) ,
length(): int,
charAt(int index): char
endsWith( MyString suffix) : boolean
indexOf( char ch): int
indexOf( MyString str) int
indexOf( char ch, int fromIndex): int
replace( char target, char replacement): MyString
substring( int beginIndex) : MyString
substring( int beginIndex, int endIndex): MyString
toUpperCase(): MyString
toLowerCase(): MyString
toString(): String
How would I do this? I am completely lost
Explanation / Answer
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) {
System.out.println("Enter the string:");
Scanner sc=new Scanner(System.in);
String MyString=sc.nextLine();
/* char ch=charat(MyString,8); //for checking output
int len=Length(MyString); //here you can input any string and all the functions work accordingly
boolean end=endsWithSuffix(MyString,"rld"); //arguments are to be passed according to the input. Here I took input as hello world
int ind=indexOfCharacter(MyString,'w');
int in=indexOfString(MyString,"orld");
int inc=indexOfCharacterFromString(MyString,'w','l');
String rep=replaceString(MyString,"llo","abc");
String sub=substring(MyString,3);
String subs=substringmid(MyString,3,7);
System.out.println(len);
System.out.println(ch);
System.out.println(end);
System.out.println(ind);
System.out.println(in);
System.out.println(inc);
System.out.println(rep);
System.out.println(sub);
System.out.println(subs);
//toLowerCase(MyString);
toUpperCase(MyString); */
}
public static int Length(String str)
{
int length=0;
char[] strCharArray=str.toCharArray();
for(char c:strCharArray)
{
length++;
}
return length;
}
public static char charat(String str, int in) {
char[] strCharArray=str.toCharArray();
char ch=strCharArray[in];
return ch;
}
public static boolean endsWithSuffix(String str, String suffix) {
char[] chStr = str.toCharArray();
char[] chSuf = suffix.toCharArray();
int l1=Length(str);
int l2=Length(suffix);
int l=l2,flag=0;
for(int i=0;i<l;i++)
{
if(chStr[l1-l2]==chSuf[i])
{
flag=1;
}
l2++;
}
if(flag==0) {
return false;
}
return true;
}
public static int indexOfCharacter(String str, char ch) {
char[] strCharArray=str.toCharArray();
for(int i=0;i<Length(str);i++) {
if(strCharArray[i]==ch) {
return i;
}
}
return 0;
}
public static int indexOfString(String str, String ch) {
boolean match;
char[] strCharArray=str.toCharArray();
char[] strCharArray2=ch.toCharArray();
for (int i = 0; i < Length(str) - Length(ch) + 1; ++i)
{
match = true;
for (int j = 0; j <Length(ch); ++j)
{
if (strCharArray[i + j] != strCharArray2[j])
{
match = false;
break;
}
}
if (match) return i;
}
return -1;
}
public static int indexOfCharacterFromString(String str, char ch,char in) {
int a=indexOfCharacter(str,ch);
int b=indexOfCharacter(str,in);
return a-b;
}
public static String replaceString(String actual, String str, String req) {
char[] strCharArray=str.toCharArray();
char[] strCharArray2=req.toCharArray();
char[] strCharArray3=actual.toCharArray();
int k=indexOfString(actual,str);
int l=Length(str);
for(int i=0;i<l;i++)
{
strCharArray3[k]=strCharArray2[i];
k++;
}
String Ans = String.valueOf(strCharArray3);
return Ans;
}
public static String substring(String str, int index)
{
char[] strCharArray=str.toCharArray();
char[] array=new char[100];
int l=Length(str);
int x=index;
for(int i=0;i<l-x;i++)
{
array[i]=strCharArray[index];
index++;
}
String Ans=String.valueOf(array);
return Ans;
}
public static String substringmid(String str, int beg, int end)
{
char[] strCharArray=str.toCharArray();
char[] array=new char[100];
int x=beg;
for(int i=0;i<end-x+1;i++)
{
array[i]=strCharArray[beg];
beg++;
}
String Ans=String.valueOf(array);
return Ans;
}
/*public static void toLowerCase(String a){ //this function will work if we give input in upper case. In that case we have to change arguements of other functions as required.
for (int i = 0; i< a.length(); i++){
char aChar = a.charAt(i);
if (65 <= aChar && aChar<=90){
aChar = (char)( (aChar + 32) );
}
System.out.print(aChar);
}
} */
public static void toUpperCase(String a){
for (int i = 0; i< a.length(); i++){
char aChar = a.charAt(i);
if (97 <= aChar && aChar<=122){
aChar = (char)( (aChar - 32) );
}
System.out.print(aChar);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.