Click here to Skip to main content
15,887,936 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Eg 1: Input: a1b10
       Output: abbbbbbbbbb
Eg: 2: Input: b3c6d15
          Output: bbbccccccddddddddddddddd
The number varies from 1 to 99.


What I have tried:

Java
static void printCharSeq(char c, int num) {
		while (num > 1) {
			System.out.print(c);
			num--;
		}
	}

void convert(String str) {
		char c[] = str.toCharArray();
		for (int i = 0; i < c.length; i++) {
			int val = 0;
			if ((c[i] >= '0' && c[i] <= '9')) {
				// val=c[i]-48;
				try {
					if (c[i + 1] >= '0' && c[i + 1] <= '9') {
						String a1 = String.valueOf(c[i] - 48);
						String a2 = String.valueOf(c[i + 1] - 48);
						val = Integer.parseInt(a1 + a2);
						// System.out.println(val);
						printCharSeq(c[i - 1], val);
						i++;
					} else {
						val = c[i] - 48;
						printCharSeq(c[i - 1], val);
					}
				} catch (ArrayIndexOutOfBoundsException ex) {
					val = c[i] - 48;
					printCharSeq(c[i - 1], val);
				}
			} else {
				System.out.print(c[i]);
				// printCharSeq(c[i], val);
			}
		}
	}


Here This logic works perfectly but when i try to 
input : v99t100cvrx2
Output : vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvttttttttttcvrxx

it prints t 10 times too. i don't know ether it is correct or wrong. please help me to find it out the logic is correct for different inputs.
Posted
Updated 13-Mar-23 16:44pm

Quote:
it prints t 10 times too. i don't know ether it is correct or wrong.

I think it is correct because you also state that:
Quote:
The number varies from 1 to 99.

Lets guess that the code handle numbers with only 2 digits.
If you want more than 2 digits, you have to change the code accordingly.
Rather than having code to handle 1 digit and code to handle 2 digits, I would use a loop.

Eg 1: Input: a1b10
       Output: abbbbbbbbbb

this coding is RLE Run Length Encoding
Run-length encoding - Wikipedia[^]
 
Share this answer
 
In order to convert a sequence of numeric characters to an integer value you need a simple loop like this:
Set value = 0
For each character:
    if character is a number:
        set tempval = character - '0'
        multiply value by 10
        add tempval to value
    else:
        print the current character value times
// continue with the remaining processing.
 
Share this answer
 
The below code is the complete code of Character Sequence of a given Number and the condition given that the limit is between 1-99 it does not exceeds the limit,if it does it takes the first two digit of the number
For example, if its 100=10 times it prints the respective character,This program is based on the array's positions,before you do,you have to consider it otherwise it spoil the entire code

Java
import java.util.Scanner;
class CharSequence
{
static void PrintSeq(char c, int num) 
{
		while (num > 1) 
		{
			System.out.print(c);
			num--;
		}
}

static void ToConvert(String str) 
   {
		char c[] = str.toCharArray();
		
		for (int i = 0; i < c.length; i++) 
		{
			int val = 0;
			
			if ( c[i] >= '0' && c[i] <= '9' ) 
			{
				try 
				{
					if ( c[i + 1] >= '0' && c[i + 1] <= '9' ) 
					{
					//Used to print more than 9 values
					String a1 = String.valueOf(c[i] - 48);
					String a2 = String.valueOf(c[i + 1] - 48);
						
						val = Integer.parseInt(a1 + a2);
						
						PrintSeq(c[i - 1], val);
						
						i++;
					}
					else 
					{
						//Used to print 0-9 values
						val = c[i] - 48;
						PrintSeq(c[i - 1], val);
					}
				} 
				catch (ArrayIndexOutOfBoundsException ex) 
				{
					val = c[i] - 48;
					PrintSeq(c[i - 1], val);
				}				
			} 
			else 
			{
				System.out.print(c[i]);				
			}
		}
	}
	public static void main(String args[]) throws Exception
	{
		Scanner s=new Scanner(System.in);
		
		String wrd=s.nextLine();
		ToConvert(wrd);
	}
}
 
Share this answer
 
Comments
Patrice T 21-Jun-18 15:01pm    
This adds nothing, you dome a plagiary of code in question .
DeenaDhayaluM 21-Jun-18 20:17pm    
Yeah,I did apologize for it,I just try to give a complete code without any fault in it. Hope it may help some other to solve and it gives a clear idea how the data flows.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900