Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/main/java/com/thealgorithms/strings/ComplexNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.Scanner;

public class ComplexMultiply {
/**
* Multiplies two complex numbers given as strings in "a+bi" format.
* Parses the strings, performs multiplication, and returns the product.
*
* num1 First complex number string
* num2 Second complex number string
* return the product as a string in "x+yi" format
*/
public static String multiply(String num1, String num2) {
int n1 = num1.indexOf('+');
int a = Integer.parseInt(num1.substring(0, n1));
int b = Integer.parseInt(num1.substring(n1 + 1, num1.length() - 1)); // exclude 'i'

int n2 = num2.indexOf('+');
int c = Integer.parseInt(num2.substring(0, n2));
int d = Integer.parseInt(num2.substring(n2 + 1, num2.length() - 1)); // exclude 'i'

int real = a * c - b * d; // real part of product
int imag = a * d + b * c; // imaginary part of product

return real + "+" + imag + "i";
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Prompt user to enter first complex number
System.out.print("num1: ");
String num1 = scanner.nextLine();

// Prompt user to enter second complex number
System.out.print("num2: ");
String num2 = scanner.nextLine();

// Perform multiplication and display result
String result = multiply(num1, num2);
System.out.println("Output: " + result);

scanner.close();
}
}
58 changes: 58 additions & 0 deletions src/main/java/com/thealgorithms/strings/Removing stars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.util.Scanner;

public class RemoveStars {

/**
* Removes stars from the input string by deleting the closest non-star character to the left of each star along with the star itself.
*
* The input string containing stars (*)
* The resulting string after all stars and their closest left characters are removed
*/
public static String removeStars(String s1) {
// Use StringBuilder for efficient string manipulation
StringBuilder sc = new StringBuilder();

// Loop through each character in the input string
for (int i = 0; i < s1.length(); i++) {
char ch = s1.charAt(i);

if (ch == '*') {
// When star is found, remove last character from sc if it exists
if (sc.length() > 0) {
sc.deleteCharAt(sc.length() - 1);
}
} else {
// Append non-star characters to sc
sc.append(ch);
}
}

// Convert StringBuilder to string and return
return sc.toString();
}

public static void main(String[] args) {
// Create a Scanner object to read user input from the console
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter a string containing stars
System.out.print("Input: ");

// Read the entire line entered by the user
if (scanner.hasNextLine()) {
String input = scanner.nextLine();

// Call the removeStars method to process the string input
String output = removeStars(input);

// Print the resulting string after removing stars and their left characters
System.out.println("Output: " + output);
} else {
// Inform user if no input was provided
System.out.println("No input provided.");
}

// Close the scanner to free resources
scanner.close();
}
}
Loading