Menu
Topics Index
...
`

Suffix every character in the given character array


If you need explanation Read this topic

If you need Answer Take test on this topic

Given the input word as a character array, then write a program to suffix every character in the input word.

Input (char[], char) Output (char[])
{'B', 'o', 'o', 'k'}, 'T' {'B', 'T', 'o', 'T', 'o', 'T', 'k', 'T'}
{'D', 'h', 'o', 'n', 'i'}, 'E' {'D', 'E', 'h', 'E', 'o', 'E', 'n', 'E', 'i', 'E'}
{'M', 'o', 't', 'h', 'e', 'r'}, 'O' {'M', 'O', 'o', 'O', 't', 'O', 'h', 'O', 'e', 'O', 'r', 'O'}
{'W', 'e', 'l', 'c', 'o', 'm', 'e'}, 'R' {'W', 'R', 'e', 'R', 'l', 'R', 'c', 'R', 'o', 'R', 'm', 'R', 'e', 'R'}
{'T', 'a', 'b', 'l', 'e'}, 'P' {'T', 'P', 'a', 'P', 'b', 'P', 'l', 'P', 'e', 'P'}



class SuffixEveryCharacterInArray
{
    public static void main(String s[])
    {
        char input[] = {'B', 'o', 'o', 'k'};
        char result[] = suffixEveryCharacter(input, 'T');
        System.out.print("After suffixing every character in the input word : ");
        for (char ch : result)
        System.out.print(ch + " ");

    }


    public static char[] suffixEveryCharacter(char[] input, char suffix) {
    }

}
Topic: Learn Arrays And Loops

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Iterate over each character in the chararcter array using for each loop and append every character to the String Builder object followed by append given suffix in every iteration...Finally take the Strnig version of the String Builder and convert into a char array and return it.                    

   StringBuilder t=new StringBuilder();
    for (char i:array){
        t.append(i).append(suffix);
    }
    return t.toString().toCharArray();

Posted by Shashanka Mogaliraju    2015-03-19 08:33:28


For suffixing every letter in input word we have to create array double of size mentioned in input.

Char[] result=new char[input.length*2]

After that we have to use for loop from 1 to length of result array.

For 0th indexed element it should be placed 0th in result.

1st element should be character given as input like T.

2nd element of input array should be placed 4th in result I.e. index *2 is location of every input element in result array.

So for every even iteration of the for loop The element of input should be placed at I*2 position and for odd iteration of loop character given as input should be copied.

If(i%2==0)
Result[I*2]=input [I]//even element
Else
Result[I]=suffix//odd element

Posted by Mânïshå Mùlchåndânï    2015-03-19 08:33:56


This dose is now closed and the winners are . The 'lucky liker' is N Rajasekhar Reddy. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-20 07:19:48


© meritcampus 2019

All Rights Reserved.

Open In App