Skip to main content

Java program to remove all whitespaces from entered string.




import java.util.*;
class RemoveWhiteSpaceFromString
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your string");
String s=sc.nextLine();
System.out.println();
System.out.println("Removing Whitespaces");
System.out.println();
s=s.replaceAll("\\s+","");
System.out.println(s);
}
}



Comments

Popular posts from this blog

Find number of vowels in a string.

#include<stdio.h> int main() { char str[25]; printf("Enter string.\n"); gets(str); int i = 0; int count = 0; while(str[i] != '\0') { if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') { count++; } i++; } printf("\nNo. of vowels is %d",count); return 0; }

Check if two strings are equal.

In this post we will see how to check if two strings are equal in C.For this we will use strcmp function. It compares two strings  lexicographically i.e character by character, if the two character matches moves to next character and compares it. It returns 0 if both strings are equal. #include<stdio.h> #include<string.h> int main() { char str[25]; printf("Enter string 1.\n"); gets(str); char str1[25]; printf("\nEnter string 2.\n"); gets(str1); int i = strcmp(str, str1); if(i==0) { printf("Equal.\n"); } else { printf("Not Equal.\n"); } return 0; }

Java program on two dimension Matrix of String or Characters.

import java.util.*; class CharArray { public static void main(String args[]) { String a[][]; a=new String[3][3]; int i=0,j=0; Scanner sc=new Scanner(System.in); System.out.println("Enter your characters"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { a[i][j]=sc.next(); } } System.out.println(); for(i=0;i<3;i++) { for(j=0;j<3;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } } /* Here I have used 3*3 matrix, you can also form any multi-dimension matrix by providing its size using scanner.*/