dark
given a string, return a string where for every char in the original, there are two chars.

Solution of ” given a string, return a string where for every char in the original, there are two chars.” Problem.

   <ul>
   <li>doubleChar("The") --> "TThhee"</li>
   <li>doubleChar("AAbb") --> "AAAAbbbb"</li>
    <li>doubleChar("Hi-There") --> "HHii--TThheerree"</li>
    </ul>

Example explanation:

In this problem, we will use the following substrings and loops

length()

charAt

for loop

Manipulating Characters in a String:

To examine string there are many methods of string class such as searching character, string or substring in the string, measure the length, and other tasks.

Length():

Description:

This method returns the length of this string. It is equal to the number that is in Unicode characters in the string.

Syntax:

The syntax of this method is:

public int length()

Parameters:

Here is the detail of parameters −

  • NA

Return Value

  • This method returns the the length of the sequence of characters represented by this object.

CharAt():

Description:

This method will return the character placed at the String’s specified index. The string indexes start from zero.

Syntax:

The syntax of this method is:

public char charAt(int index)

Parameters:

Here is the detail of parameters −

  • index − Index of the character to be returned.

For loop:

Initialization:

The initial statement is executed when the controls come into the loop. Here, you can initialize the new variable or you can use an already initialized variable. this is an optional condition.

Condition:

Condition is the second part of the loop; the loop is executed after testing the condition. control will continue to execute the statements until the condition is true. It will return a boolean value whether true or false. It is an optional condition.

Statement:

            The statement is the third part of the loop it is executed until the condition is true.

Increment/Decrement:

It increments or decrements the variable value. It is an optional condition.

Example solution:

public class String2
	{
	    /**






	     * @param str not null string
	     * @return string with double chars
	     */
	    public static String doubleChar( String str )
	    {
	        String result = "";
	
	        for ( int i = 0; i < str.length(); i++ )
	        {
	            result = result + str.charAt( i ) + str.charAt( i );
	        }
	
	        return result;
	    }

Total
1
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
Discord chat exporter

How to export your chats using Discord chat exporter

Next Post
debconf: delaying package configuration, since apt-utils is not installed

Try the solution of debconf: delaying package configuration, since apt-utils is not installed

Related Posts