Skip to content

Instantly share code, notes, and snippets.

@theArjun
Created January 26, 2020 12:12
Show Gist options
  • Select an option

  • Save theArjun/7602dc617c47b667e84785c0c956c96d to your computer and use it in GitHub Desktop.

Select an option

Save theArjun/7602dc617c47b667e84785c0c956c96d to your computer and use it in GitHub Desktop.
New String Data Type
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
MString ms = new MString("Unassigned");
System.out.print("Do you want to initialize MString through length (Y/N) : ");
if( sc.next().toUpperCase().toCharArray()[0] == 'Y') {
System.out.print("Approximate size of your MString : ");
int len = sc.nextInt();
ms = new MString(len);
System.out.print("Enter desired string : ");
String input_string_append = sc.next();
ms.append(input_string_append);
} else {
System.out.print("Enter desired string : ");
String input_string = sc.next();
ms = new MString(input_string);
}
while (true) {
System.out.println("\nPlease read the instructions.");
System.out.println("Select the number to perform the required operations.");
// length()
System.out.println("1. To Calculate the length of the string.");
// charAt(int desired_index)
System.out.println("2. To return the character at given index.");
System.out.println("3. To convert the string into uppercase.");
System.out.println("4. To replace character at given index.");
System.out.println("5. To compare two MStrings.");
System.out.println("6. To append a character.");
System.out.println("7. To append a string.");
System.out.println("8. To insert character at given index.");
System.out.println("9. To display the string.");
System.out.println("\nPress any other character to exit.");
System.out.println("\nEnter respective number for operations.");
int selection = sc.nextInt();
switch (selection) {
case 1:
System.out.println("Length : " + ms.length());
break;
case 2:
System.out.print("Enter the index to return respective character : ");
int return_index = sc.nextInt();
System.out.println("Character in Index " + return_index + " : " + ms.charAt(return_index));;
break;
case 3:
System.out.println("The uppercased string : " + ms.toUpperCase());
break;
case 4:
System.out.print("Enter the index to replace the character : ");
int index_replacement = sc.nextInt();
System.out.print("Enter the character as replacement : ");
ms.replaceCharAt(index_replacement, sc.next().toCharArray()[0]);
break;
case 5:
System.out.print("Enter another string to compare : ");
System.out.println("Comparision Result : " + ms.equals(new MString(sc.next())));
break;
case 6:
System.out.print("Enter character to append : ");
ms.append(sc.next().toCharArray()[0]);
System.out.print("After Appending : ");
ms.display();
break;
case 7:
System.out.print("Enter string to append : ");
ms.append(sc.next());
System.out.print("After Appending : ");
ms.display();
break;
case 8:
System.out.print("Enter index to insert the character : ");
int index_insertion = sc.nextInt();
System.out.print("Enter character to be inserted : ");
ms.insert(sc.next().toCharArray()[0], index_insertion);
System.out.print("After Insertion : ");
ms.display();
break;
case 9:
ms.display();
break;
default:
break;
}
}
}
}
import java.lang.String;
// import java.util.Arrays;
public class MString {
private char[] theChars;
private int n;
private void expand(int mustHaveCapacity) {
char[] temp = new char[2 * mustHaveCapacity];
System.out.println("Temp size : " + temp.length);
for (int i = 0; i < this.length(); i++) {
temp[i] = this.theChars[i];
}
this.theChars = temp;
System.out.println("theChars size : " + this.theChars.length);
System.out.println("inside expand");
// this.theChars = Arrays.copyOf(this.theChars, 2*mustHaveCapacity);
}
public MString(int desired_length) {
try {
this.n = desired_length;
char[] theChars = new char[n];
System.out.println("char array created.");
} catch (NullPointerException e) {
System.out.println("array is empty");
}
}
public MString(String input_text) {
n = 1000;
theChars = new char[n];
char[] temp = input_text.toCharArray();
// No Minus 1
for (int i = 0; i < temp.length; i++) {
theChars[i] = temp[i];
}
}
public int length() {
int i = 0;
while (i < this.n) {
if(theChars == null) {
return 0;
}
if (theChars[i] == '\0') {
break;
}
i++;
}
return i;
}
public char charAt(int desired_index) {
return theChars[desired_index];
}
public String toUpperCase() {
return (new String(theChars)).toUpperCase();
}
public void replaceCharAt(int index, char ch) {
theChars[index] = ch;
}
public boolean equals(MString other) {
boolean assumption = true;
char[] other_chars = other.theChars;
int other_length = other.length();
if (this.length() != other_length) {
assumption = false;
return assumption;
}
for (int i = 0; i < other_length; i++) {
if (this.theChars[i] != other_chars[i]) {
assumption = false;
return assumption;
}
}
return assumption;
}
public void append(char ch) {
if (this.length() >= this.n) {
System.out.println("Length before Expanding : " + this.n);
expand(100);
System.out.println("Length after Expanding : " + this.n);
}
// The last character was stored in length-1. Hence we append the character at
// index:length.
this.theChars[this.length()] = ch;
}
public void append(String str) {
// if (this.length() >= this.n) {
// expand(this.n);
// }
char[] str_char_array = str.toCharArray();
int start_index = this.length();
for (int i = 0; i < str.length(); i++) {
this.theChars[start_index] = str_char_array[i];
start_index++;
}
}
public void insert(char character, int index) {
// if (this.length() >= this.n) {
// expand(this.n);
// }
int loop_limit = this.length();
for (int i = loop_limit; i > index; i--) {
this.theChars[i] = theChars[i - 1];
}
this.theChars[index] = character;
}
public void display() {
try {
int i = 0;
while (i < this.n) {
if (this.theChars[i] == '\0') {
break;
}
System.out.print("" + this.theChars[i]);
i++;
}
System.out.println();
} catch (Exception exc) {
System.out.println("\nDisplay Error");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment