0% found this document useful (0 votes)
6 views4 pages

L32Number-Based Strings in Java

Uploaded by

dp148026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

L32Number-Based Strings in Java

Uploaded by

dp148026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

L32:Number-Based Strings in

Java
Concept Explanation
1. Number-Based Strings:

A string like "A10B12C3D4" contains characters followed by numbers. Each


character should be printed a number of times corresponding to the
number that follows it.

Example: "A10" means A should be printed 10 times, "B12" means B

should be printed 12 times, etc.

2. ASCII Values:

Characters and numbers in a string have ASCII values.

For example, ASCII value of 1 is 49 , 0 is 48 , and A is 65 .

Step-by-Step Solution
1. Input Handling:

Read the input string from the user.

Example input: "A10B12C3D4" .

2. Segregate Characters and Numbers:

Iterate through the string and separate characters from numbers.

Use conditions to check if a character is an alphabet or a digit.

3. Convert Characters to Numbers:

Convert the sequence of digits into an integer using ASCII arithmetic.

Formula: num = num * 10 + (int_of_char - 48)

4. Implementation:

Write code to read the string, segregate characters and numbers,


convert the numbers, and then print the characters the specified
number of times.

Detailed Code Explanation

L32:Number-Based Strings in Java 1


import java.util.Scanner;

public class NumberBasedStrings {


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

// Read input string


System.out.println("Enter the string:");
String input = scanner.next();

// Convert string to character array


char[] arr = input.toCharArray();
int num = 0; // To store the number part
char ch = ' '; // To store the character part

for (int i = 0; i < arr.length; i++) {


if (arr[i] >= '0' && arr[i] <= '9') { // Check
if it's a digit
num = num * 10 + (arr[i] - '0'); // Convert
char digit to integer
} else { // It's a character
// Print the previous character the specifi
ed number of times
if (i > 0) {
for (int j = 0; j < num; j++) {
System.out.print(ch);
}
}
// Reset num and update the character
num = 0;
ch = arr[i];
}
}

// Print the last character the specified number of


times
for (int j = 0; j < num; j++) {
System.out.print(ch);

L32:Number-Based Strings in Java 2


}
}
}

Key Points
1. Initialization:

num is initialized to 0 to hold the numeric value.

ch is initialized to a space or a placeholder character.

2. Iteration:

The loop iterates through each character in the input string.

If the character is a digit, it updates num .

If the character is an alphabet, it prints the previous character num times


and updates ch to the current character.

3. Printing:

After exiting the loop, it prints the last character the specified number of
times.

Practical Usage
Example:

Input: "A10B12C3D4"

Output: AAAAAAAAAABBBBBBBBBBBCCCCDDDD

This approach is useful for problems involving encoded messages or data


compression where a format like A10 is used to represent AAAAAAAAAA .

Common Mistakes and Tips


1. Index Handling:

Ensure the loop handles the last character and number correctly by
printing after the loop ends.

2. Resetting Values:

Reset num after printing to avoid incorrect accumulations.

3. Boundary Conditions:

L32:Number-Based Strings in Java 3


Handle cases where the input might not follow the expected pattern
strictly.

L32:Number-Based Strings in Java 4

You might also like