Coding Challenge Day- 24: Problem 1: Push ‘null’ to end of String array

Hello! So I’m doing a 30 day coding challenge where I solve a few questions every day and thought of posting them here on my blog so that you guys can join the challenge too!

Welcome to Coding challenge Day 24: Problem 1! Be sure to post your answers, queries etc in the comments!

Problem: Push ‘null’ to end of String array

Sample: String [] array= {“Horse”, null, “Cow”, “Hen”, null, “Pig”};
Otuput: “Horse”, “Cow”, “Pig”, null, null

Solution:

package string;

public class Program014null_at_end {
	
	public static void main (String [] args){
		String [] array= {"Horse", null, "Cow", "Hen", null, "Pig"};
		
		for(int i=0; i<array.length; i++){
			if (array[i]==null){
				for(int j=i+1; j<array.length; j++){
					if(array[j]!=null){
						array[i]=array[j];
						array[j]= null;
						break;
					}
				}
			}
		}
		
		for(String s: array){
			System.out.print(s +" ");
		}
	}

}

Download Code

Happy Learning!!

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close