Posts

Showing posts from April, 2021

Try:- Quick way to check if all the characters of a string are same

 // Java program to find whether the String // has all same characters or not. import java.io.*; public class GFG{ static boolean allCharactersSame(String s) { int n = s.length(); for (int i = 1; i < n; i++) if (s.charAt(i) != s.charAt(0)) return false; return true; } // Driver code static public void main (String[] args){ String s = "aaa"; if (allCharactersSame(s)) System.out.println("Yes"); else System.out.println("No"); } } // This Code is contributed by vt_m.

First non-repeating character using one traversal of string | Set 2

 import java.util.*; import java.io.*; import java.lang.*; public class HelloWorld{          static int firstnonrepeat(String str){                  int no_of_char = 256;                  int arr[] = new int[no_of_char];         for(int i = 0;i<no_of_char;i++){             arr[i] = -1;  //every time any no come it is -1         }                  for(int j = 0;j<str.length();j++){                          if(arr[str.charAt(j)]==-1){                 arr[str.charAt(j)] = j;             }else{                 arr[str.charAt(j)] = -2;             }...
 Integer.MAX_VALUE Integer .  MAX_VALUE  represents the maximum positive  integer  value that can be represented in 32 bits (i.e., 2147483647 ). This means that no number of type  Integer  that is greater than 2147483647 can exist in Java. Java.lang.Math.min() Method The  java.lang.Math.min(int a, int b)  returns the smaller of two int values. That is, the result is the value closer to negative infinity. If the arguments have the same value, the result is that same value. public static int min(int a, int b)

Axis bank question

Axis bank question Q) public class HelloWorld{      public static void main(String []args){ String a = "programming"; String b = "ra"; char [] n = a.toCharArray(); char [] m = b.toCharArray(); for(int i = 0 ;i<a.length();i++){     for(int j = 0;j<b.length();j++){         if(n[i]==m[j]){             i++;         }     }     System.out.print(n[i]); }      } } Ans of flow chart k = 1,4,9,6,5,6 p = 1,4,6,6,0,0 check the reminder which is the last term in this case 0 that's why 0 % 10 means no remainder remaining ans 0 Ans of 1Q) sum = 1 , 3 , 5 ,  c = c  sn = n(n+1)/2 we have to find the sum of the nth term  formula = sm = n(n+1)/2;

Java Program to Check Whether the String Consists of Special Characters

 // Java Program to Check Whether String contains Special // Characters Using Character Class // Importing input output classes import java.io.*; // Main class class GFG { // Method 1 // Main driver method public static void main(String[] args) { // Declaring and initializing count for // special characters int count = 0; // Input custom string String s = "!#$GeeeksforGeeks.Computer.Science.Portal!!"; // Iterating through the string // using standard length() method for (int i = 0; i < s.length(); i++) { // Checking the character for not being a // letter,digit or space if (!Character.isDigit(s.charAt(i)) && !Character.isLetter(s.charAt(i)) && !Character.isWhitespace(s.charAt(i))) { // Incrementing the countr for spl // characters by unity count++; } } // When there is no special character encounterd if (count == 0) // Display the print statement System.out.println...

Java Program to Print all Unique Words of a String

 public class HelloWorld{      public static void main(String []args){         String str = "Welcome to geeks for geeks";         int count;//if same word milga to skip karna ka lia count++ karna pada ga                  String[] words = str.split("\\W");                  for(int i = 0;i<words.length;i++){                          count = 1;                          for(int j = i+1;j<words.length;j++){                 if(words[i].equalsIgnoreCase(words[j])){                     count++;                     words[j]="";               ...

Remove the dublicate word from string

 public class HelloWorld{      public static void main(String []args){         String str = "Welcome to geeks for geeks";         int count;//if same word milga to skip karna ka lia count++ karna pada ga                  String[] words = str.split("\\W");                  for(int i = 0;i<words.length;i++){                          count = 1;                          for(int j = i+1;j<words.length;j++){                 if(words[i].equalsIgnoreCase(words[j])){                     count++;                     // words[j]="";             ...

Program to count the total number of vowels and consonants in a string.

 public class HelloWorld{      public static void main(String []args){                  String file = "This is a really simple sentence";                  int count = 0;         int count1 = 0;                  for(int i =0 ;i<file.length();i++){             char v = file.charAt(i);             if(v=='a' || v== 'e' || v=='i' || v=='o' || v=='u'){                count++;             }else             {                 count1++;             }         }                   System.out.println("it is a vowels "+count); ...

calculate how many punctuation in string

  public   class  punctuation {        public   static   void  main (String [] args) {            //Stores the count of punctuation marks             int  countPuncMarks =  0 ;           String str =  "Good Morning! Mr. James Potter. Had your breakfast?" ;            for  ( int  i =  0 ; i < str.length(); i++) {                    //Checks whether given character is punctuation mark             ...

remove special character from String

Q ) Remove Special Character from String.  public class HelloWorld{      public static void main(String []args){                  String file = "Good Morning! Mr. James Potter. Had your breakfast?";         String remove ="";                  for(int i =0 ;i<file.length();i++){             char a = file.charAt(i);             if(a=='!' || a== '.' || a=='?'){             }else{                 remove = ""+ a;                 System.out.print(remove);             }         }              } }

Java program to capitalize first letter of each word in a string

 import java.lang.*; public class HelloWorld{      public static void main(String []args){         String s = "biplav kumar mazumdar"; //aadd space                  s = " "+s;                  String f = "";                  for(int i = 0;i<s.length();i++){             char ch = s.charAt(i);             if(ch==' '){                 f = f+ch;                  i++;                  ch = s.charAt(i);                  f = f+Character.toUpperCase(ch);             }else{                 f = f+ch;       ...