Found 7442 Articles for Java

Java Program to Sort a String

AmitDiwan
Updated on 29-Mar-2022 11:21:25

3K+ Views

In this article, we will understand how to sort a string. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). Strings are a sequence of charactersBelow is a demonstration of the same −Suppose our input is −Input string: javaprogramThe desired output would be −String after sorting is: [a, a, a, g, j, m, o, p, r, r, v]AlgorithmStep 1 - START Step 2 - Declare a string value namely input_string, a character array charArray, char value name temp and an int value namely string_size. Step 3 - Define the values. Step 4 ... Read More

Java program to reverse a string using stacks

Aishwarya Naglot
Updated on 05-Jun-2025 11:53:14

2K+ Views

Stack is a linear data structure where we can store elements. It uses the LIFO (last in, first out) principle, which means the item we add last will be the first one to be removed. In this article, we will understand how to reverse a string using stacks. Let's take an example: Input: String input_string = "Java Program"; Output: String reversed_string = "margorP avaJ"; Ways to Reverse a String Using Stacks Below are the different approaches to reverse a string using stacks: Reverse a string using built-in stack methods ... Read More

Java Program to Reverse a String

Shriansh Kumar
Updated on 25-Sep-2024 18:03:35

2K+ Views

Given a String, let's say "str", write a Java program to reverse it. In reverse string operation, we need to display the individual characters of string backwards or from right to left. Here, String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). Let's understand the problem with an example − Example Scenario: − Input: str = "Java Program"; Output: reversed_string = margorP avaJ For better understanding refer to the below illustration − Reverse String .main { background-color: rgb(151, 231, 89); ... Read More

Java program to replace a character at a specific index

Aishwarya Naglot
Updated on 30-May-2025 18:47:21

4K+ Views

In this article, we'll learn how to replace a character at a specific index in a string. Strings in Java are sequences of characters enclosed in double-quotes (" "). We use strings to represent text in our programs. Problem Statement You are given a string, an index, and a character. Your task is to replace the character at the specified index in the string with the new character. Input string: Java Programming, Index: 6 Output: Java P%ogramming Different Approaches There are multiple ways to achieve this in Java. Below are two common approaches: Using substring() ... Read More

Java Program To Determine If a Given Matrix is a Sparse Matrix

Aishwarya Naglot
Updated on 05-Jun-2025 14:42:35

434 Views

What is a Sparse Matrix? A matrix is said to be a sparse matrix if most of the elements of that matrix are 0. It means that the number of non-zero elements has a lesser count than the number of zero elements.Let's say we have a matrix of size 3*3, which means it has 9 elements. If 5 or more of those elements are 0, then we can say that the matrix is sparse. A = [ 0 2 0 ] [ 0 5 4 ] [ 0 0 0 ] ... Read More

Java Program to display upper triangular matrix

Shriansh Kumar
Updated on 30-Sep-2024 16:07:07

1K+ Views

In this article, we will understand how to display an upper triangular matrix of a given matrix in Java. A matrix has row and column arrangement of its elements. A matrix with m rows and n columns can be called as m × n matrix. And, the term upper triangular matrix refers to a matrix whose all elements below the main diagonal are 0. Example Scenario: Input: matrix = { 2, 1, 4 }, { 1, 2, 3 }, { 3, 6, 2 } Output: upptri_mat = 2 1 4 0 2 3 0 0 2 Using ... Read More

Java Program to Recursively Linearly Search an Element in an Array

Aishwarya Naglot
Updated on 05-Jun-2025 14:59:33

3K+ Views

Linear Search on a Java Array using RecursivelyLinear search is an algorithm that we use to check if an element is present in an array or not. It checks all the elements of the array one by one until it finds the element or reaches the end of the array. Recursion is a way where a function calls itself to solve a certain problem until it reaches a base case. In this article, we will understand how to recursively linearly search an element in an array. Let's see an example: Input: int [] arr = {1, 2, 3, 4, 5}; ... Read More

Java program to multiply two matrices using multi-dimensional arrays

AmitDiwan
Updated on 15-Nov-2024 18:48:21

1K+ Views

In this article, we will understand how to multiply two matrices using multi-dimensional arrays in Java. The matrix has a row and column arrangement of its elements. A matrix with m rows and n columns can be called as m × n matrix. Individual entries in the matrix are called elements and can be represented by a[i][j] which suggests that the element a is present in the ith row and jth column. Problem Statement Write a program in Java to multiply two matrices using multi-dimensional arrays. Below is a demonstration of the same − Input First matrix: 2 3 4 ... Read More

Java Program to Add Two Matrix Using Multi-Dimensional Arrays

Shriansh Kumar
Updated on 30-May-2025 17:09:49

1K+ Views

For two given matrices, each of size m x n, we need to write a Java program to add them. Amatrix has a row and column arrangement of its elements. A matrix with m rows and n columns can be called as m x n matrix. Individual entries in the matrix are called elements and can be represented by a[i][j], which means that the element a is present at the ith row and jth column. Matrix is an example of multi-dimensional array. In Java, a multi-dimensional array is an array of arrays. It is used to store data within a ... Read More

Java Program to Interchange the Diagonals

Manisha Chand
Updated on 22-Aug-2025 15:28:20

303 Views

In this article, we will understand how to interchange the diagonal elements of a given matrix in Java. A matrix is a two-dimensional array made up of rows and columns. A matrix with m rows and n columns is called an m × n matrix. Each element in the matrix is represented by a[i][j], which means that the particular element a is present in the i-th row and j-th column. Problem Statement Write a Java program to interchange the elements of the primary and secondary diagonals of a square matrix. Example Scenario ... Read More

Advertisements