Java 30 Days of Code - Day 6 - Strings
In this article, we will learn about Strings output question which is often asked in the interview problems.
Java 30 Days of Code - Day 6 - Strings
Problem statement -
Take an input from the user in Integer. Based upon the input parameter, accept the String values and print even/ odd characters. For an example
Input - 2
Hacker
Rank
Output
Hce akr
Rn ak
Program -
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
String string[] = new String[T];
for(int i = 0; i<T; i++){
string[i] = scan.next();
}
for(int temp = 0; temp<T; temp++){
for(int j = 0; j<string[temp].length(); j = j+2)
{
System.out.print(string[temp].charAt(j));
}
System.out.print(" ");
for(int j = 1; j<string[temp].length(); j = j+2){
System.out.print(string[temp].charAt(j));
}
System.out.println();
}
scan.close();
}
Explanation -
- Firstly, we are taking an input for the number of test cases using variable T.
- Now we are creating an array called string[] add adding a loop to capture the strings in string array.
- Afterwards, we are running nested loop, outer loop is for T, inner loop for J for traversing from 0 to length of the array.
- Second loop starts from 1 to length of the array.
- Both the statements have a line break at the end of the last loop.
2nd program -
Input String - "AAAABBCCCDDDDEEEG"
Output String - "A4B2C3D4E3G1"
Program -
public static void main(String[] args) {
String s ="AAAABBCCCDDDDEEEG";
ArrayList<Character> list = new ArrayList<>();
for(int i=0;i<s.length();i++)
{
int count=1;
if(!list.contains(s.charAt(i)))
{
list.add(s.charAt(i));
for(int j=i+1;j<s.length();j++)
{
if(s.charAt(i)==s.charAt(j))
{
count++;
}
}
System.out.print(s.charAt(i)+""+count);
}
}
}
Explanation -
- First we create an ArrayList which accepts Character return type.
- Will iterate with first loop until the string's length
- Declare count variable with value 1.
- If array list contains s.charAt(i)
- then add it to the list
- Declare another loop where j = i +1(next character)
- If char(i) == char(j)
- count++
- End J loop and print s.charAt(i) with count
Problem statement 3-
Example Input String - weelccoommee hhoommeee
Output String - welcome home
Objective is to remove consecutive characters from the string.
Solution -
We will post this solution soon
Problem statement 4 -
Example Input String - aabbbccdda
Output string -
aa-2bbb-3
cc-2
d-1
a-1
Solution -
import java.util.*;
class Main {
public static void main([] args) {
String s = "aabbbccda";
char[] c = s.toCharArray();
int length = 0;
for(char d: c){
length++;
}
HashMap<Character, Integer> hp = new HashMap<Character, Integer>();
for(int i=0;i<length;i++)
{
if(hp.containsKey(c[i])){
hp.put(c[i],hp.get(c[i])+1);
}
else {
hp.put(c[i],1);
}
}
System.out.println(hp);
}
}
Explanation -
- First we will split characters using char Array.
- Declare a variable with length = 0
- Capture the entire length of characters using for each loop.
- Declare HashMap with character and Integer data types.
- For loop from 0 to length
- If hashmap contains Key c[i]
- put c[i], hp.get(c[i] + 1)
- else put c[i],1
- System out hp
Comments
Post a Comment