Top 30+ Simple Java Programs Examples With Output

Java programing

Java Programs Based on String

Strings are similar to arrays with just a few differences. Usually, the array size is fixed, while strings can have a variable number of elements.Strings are mostly used in programming and so every coder needs to be familiar with strings and its library function.Strings library functions are there to reduce coding efforts, but a real and passionate coder would like to code in iterative way.

We have a lot of opportunities to play with strings in any manner as it is mostly used programming concept.String arrays are collection of string data for array of string.We will see different string programs.

1.Write a program to print reverse of a string.

import java.io.*;
import java.lang.*;
import java.util.*;
class stringreverse {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter a string");
String str = sc.nextLine();
StringBuffer sb = new StringBuffer(str);
sb.reverse();
System.out.println("\n Reverse of " + str + " is " + sb);
}
}
Output:
Enter a string
technocutter
Reverse of technocutter is rettuconhcet

2.Write a program to calculate the number of white space in given string.

Method-1

import java.io.*;
import java.util.*;
class Whitespacecount {
public static void main(String args[]) {
int count = 0;
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter the string:");
String str = sc.nextLine();
for (int i = 0; i < str.length(); i++) {
if (Character.isWhitespace(str.charAt(i))) count++; //checking white space in given string
}
System.out.println("Number of white spaces in given string is " + count);
}
}
Output:
Enter the string:
coding logic
Number of white spaces in given string is 1

Method-2

import java.io.*;
import java.util.*;
class Whitespacecount {
public static void main(String args[]) {
int count = 0;
Scanner sc = new Scanner(System.in);
System.out.println("\n  Enter the string:");
String str = sc.nextLine();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 32)
count++;
}
System.out.println("Number of white space in given string is " + count);
}
}
Output:
Enter the string:
coding logic
Number of white spaces in given string is 1

3.Write a program to check whether a string is palindrome or not.

import java.io.*;
import java.lang.*;
import java.util.*;
class stringpalindrome {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter a string");
String str = sc.nextLine();
StringBuffer sb = new StringBuffer(str);
sb.reverse();
String str2 = sb.toString();
if (str.compareTo(str2) == 0) {
System.out.println("\n " + str + " is palindrome");
} else {
System.out.println("\n " + str + " is not palindrome");
}
}
}
Output:
Enter a string
madam
madam is palindrome

4.Write a program to check number vowels and consonants in string.

import java.io.*;
import java.lang.*;
import java.util.*;
class vowels {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter a string \n");
String str = sc.next();
int count1 = 0, count2 = 0;
String vowels = "";
String consonants = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
ch = Character.toLowerCase(ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count1++;
vowels += str.charAt(i) + " ";
} else {
count2++;
consonants += str.charAt(i) + " ";
}
}
if (count1 > 0) {
System.out.println("\n Number of vowels are : " + count1 + " and they are : " + vowels);
}
if (count2 > 0) {
System.out.println("\n Number of Consonants are : " + count2 + " and they are : " + consonants);
}
}
}
Output:
Enter a string 
technocutter
Number of vowels are : 4 and they are : e o u e 
Number of Consonants are : 8 and they are : t c h n c t t r 

5.Write a program to remove a specific character from string.

import java.io.*;
import java.lang.*;
import java.util.*;
class removecharacter {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter a string");
String str = sc.nextLine();
System.out.println("\n Enter a character to be removed");
char ch = sc.next().charAt(0);
String new_word = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ch) {
new_word = new_word + str.charAt(i);
}
}
System.out.println("\n String without " + ch + " character : " + new_word);
}
}
Output:
Enter a string
technocutter
Enter a character to be removed
e
String without e character :tchnocuttr

6.Write a program to search a given character in string.

import java.io.*;
import java.lang.*;
import java.util.*
class search {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter a string");
String str = sc.nextLine();
System.out.println("\n Enter character to be searched");
char ch = sc.next().charAt(0);
String location = "";
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ch) {
count++;
if (location.length() > 0) {
location = location + "," + (i + 1);
} else {
location = location + "" + (i + 1);
}
}
}
if (count > 0) {
System.out.println("\n " + ch + " is present " + count + " times at " + location + " positions");
} else {
System.out.println("\n " + ch + " not found");
}
}
}
Output:
Enter a string
coding logic
Enter character to be searched
c
c is present 2 times at 1,12 positions

7.Write a program to sort a string array in descending order.

import java.io.*;
import java.lang.*;
import java.util.*;
class stringdesc {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter size of string array \n");
int n = Integer.parseInt(sc.nextLine());
String s[] = new String[n];
System.out.println("\n Enter array strings ");
for (int i = 0; i < n; i++) {
s[i] = sc.nextLine();
}
System.out.println("\n Entered array strings :");
for (int i = 0; i < n; i++) {
System.out.println(s[i]);
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (s[i].compareTo(s[j]) <= 0) {
String t = s[i];
s[i] = s[j];
s[j] = t;
}
}
}
System.out.println("\n String array in descending order :");
for (int i = 0; i < n; i++) {
System.out.println(s[i]);
}
}
}
Output:
Enter size of string array 
5
Enter array strings 
a
d
m
i
n
Entered array strings :
a
d
m
i
n
String array in descending order :
n
m
i
d
a

8.Write a program to sort string array in ascending order.

import java.io.*;
import java.lang.*;
import java.util.*;
class stringasc {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter size of string array \n");
int n = Integer.parseInt(sc.nextLine());
String s[] = new String[n];
System.out.println("\n Enter array strings ");
for (int i = 0; i < n; i++) {
s[i] = sc.nextLine();
}
System.out.println("\n Entered array strings :");
for (int i = 0; i < n; i++) {
System.out.println(s[i]);
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (s[i].compareTo(s[j]) >= 0) {
String t = s[i];
s[i] = s[j];
s[j] = t;
}
}
}
System.out.println("\n String array in ascending order :");
for (int i = 0; i < n; i++) {
System.out.println(s[i]);
}
}
}
Output:
Enter size of string array 
3
Enter array strings 
technocutter
coding
logic
Entered array strings :
technocutter
coding
logic
String array in ascending order :
coding
logic
technocutter

Leave a Reply

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