Skip to content

Commit 5f72840

Browse files
Added Removing stars program
1 parent 3eb521b commit 5f72840

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.Scanner;
2+
3+
public class ComplexMultiply {
4+
/**
5+
* Multiplies two complex numbers given as strings in "a+bi" format.
6+
* Parses the strings, performs multiplication, and returns the product.
7+
*
8+
* num1 First complex number string
9+
* num2 Second complex number string
10+
* return the product as a string in "x+yi" format
11+
*/
12+
public static String multiply(String num1, String num2) {
13+
int n1 = num1.indexOf('+');
14+
int a = Integer.parseInt(num1.substring(0, n1));
15+
int b = Integer.parseInt(num1.substring(n1 + 1, num1.length() - 1)); // exclude 'i'
16+
17+
int n2 = num2.indexOf('+');
18+
int c = Integer.parseInt(num2.substring(0, n2));
19+
int d = Integer.parseInt(num2.substring(n2 + 1, num2.length() - 1)); // exclude 'i'
20+
21+
int real = a * c - b * d; // real part of product
22+
int imag = a * d + b * c; // imaginary part of product
23+
24+
return real + "+" + imag + "i";
25+
}
26+
27+
public static void main(String[] args) {
28+
Scanner scanner = new Scanner(System.in);
29+
30+
// Prompt user to enter first complex number
31+
System.out.print("num1: ");
32+
String num1 = scanner.nextLine();
33+
34+
// Prompt user to enter second complex number
35+
System.out.print("num2: ");
36+
String num2 = scanner.nextLine();
37+
38+
// Perform multiplication and display result
39+
String result = multiply(num1, num2);
40+
System.out.println("Output: " + result);
41+
42+
scanner.close();
43+
}
44+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.util.Scanner;
2+
3+
public class RemoveStars {
4+
5+
/**
6+
* Removes stars from the input string by deleting the closest non-star character to the left of each star along with the star itself.
7+
*
8+
* The input string containing stars (*)
9+
* The resulting string after all stars and their closest left characters are removed
10+
*/
11+
public static String removeStars(String s1) {
12+
// Use StringBuilder for efficient string manipulation
13+
StringBuilder sc = new StringBuilder();
14+
15+
// Loop through each character in the input string
16+
for (int i = 0; i < s1.length(); i++) {
17+
char ch = s1.charAt(i);
18+
19+
if (ch == '*') {
20+
// When star is found, remove last character from sc if it exists
21+
if (sc.length() > 0) {
22+
sc.deleteCharAt(sc.length() - 1);
23+
}
24+
} else {
25+
// Append non-star characters to sc
26+
sc.append(ch);
27+
}
28+
}
29+
30+
// Convert StringBuilder to string and return
31+
return sc.toString();
32+
}
33+
34+
public static void main(String[] args) {
35+
// Create a Scanner object to read user input from the console
36+
Scanner scanner = new Scanner(System.in);
37+
38+
// Prompt the user to enter a string containing stars
39+
System.out.print("Input: ");
40+
41+
// Read the entire line entered by the user
42+
if (scanner.hasNextLine()) {
43+
String input = scanner.nextLine();
44+
45+
// Call the removeStars method to process the string input
46+
String output = removeStars(input);
47+
48+
// Print the resulting string after removing stars and their left characters
49+
System.out.println("Output: " + output);
50+
} else {
51+
// Inform user if no input was provided
52+
System.out.println("No input provided.");
53+
}
54+
55+
// Close the scanner to free resources
56+
scanner.close();
57+
}
58+
}

0 commit comments

Comments
 (0)