Saturday, 4 December 2021

Q13_program to find the duplicate characters in a string

remove_white_space

program to find the duplicate characters in a string

In this program, we need to find the duplicate characters in the string. Great responsibility To find the duplicate character from the string, we count the occurrence of each character in the string. If count is greater than 1, it implies that a character has a duplicate entry in the string. In above example, the characters highlighted in green are duplicate characters.

string 1

"Great responsibility"

Program

class duplicate_char {

  public static void main(String[] args)   {

    //declare string

   String str1="Great responsibility";

int count;

char str1[]=str.toCharArray();

System.out.println( "Duplicate charcters in given string" );

for (int i=0;i< str1.length;i++)

{

count=1;

forint j=i+1; j< str1.length;j++)

{

if (str1[i]==str1[j] && str1[i]!=' ')

System.out.println(str1[j]);

break;

}

}

    }

   }

output

Duplicate characters in a given string: r e t s i

Theory

Q14_Java Program to find the frequency of characters

remove_white_space

Java Program to find the frequency of characters

In this program, we need to find the frequency of each character present in the word. Picture perfect

To accomplish this task, we will maintain an array called freq with same size of the length of the string. Freq will be used to maintain the count of each character present in the string. Now, iterate through the string to compare each character with rest of the string. Increment the count of corresponding element in freq. Finally, iterate through freq to display the frequencies of characters.

string 1

"picture perfect"

Program

class FrequencyCharacter {

  public static void main(String[] args)   {

    //declare string

   String str1="Remove white spaces";

  int [] freq = new int [str.length()];

  int i, j;

  char string[] = str.toCharArray();

for (i = 0; i < str.length(); i++) {

   freq[i] = 1;

  for (j = i+1; j < str.length(); j++) {

     if (string[i] == string[j]) {

     freq[i]++;

     string[j] = '0';

   }

  }

 }

  System.out.println("Characters and their corresponding frequencies");

  for (i = 0; i < freq.length; i++)) {

   if (string[i] != ' ' && string[i] != '0')

    System.out.println(string[i] + "-" + freq[i]);

      }

    }

   }

output

Characters and their corresponding frequencies   p-2   i-1  c-2  t-2  u-1   r-2  e-3  f-1  

Theory

Friday, 3 December 2021

Q12_Program to find maximum and minimum occurring character in a string

max_min

Program to find maximum and minimum occurring character in a string

In this program, we need to count each character present in the string and find out the maximum and minimum occurring character. Grass is greener on the other side In above example, character 'a' is occurred only once in the string. So, it is minimum occurring character and is highlighted by red. Character e has occurred maximum number of times in the entire string i.e. 6 times. Hence, it is the maximum occurring character and is highlighted by green.

string 1

Remove white spaces

string 2

Removewhitespaces

Program

class max_min {

  public static void main(String[] args)   {

    //declare string

   String str1="grass is greener on the other side";

   int [] freq = new int [str.length()];

   char minChar = str.charAt(0), maxChar = str.charAt(0);

   int i, j, min, max;

   char string[] = str.toCharArray();

   for (i = 0; i < string.length; i++) {

         freq[i] = 1;

      for (j = i+1; j < string.length; j++) {

            if (string[i] == string[j] && string[i] != ' ' && string[i] != '0') {

               freq[i]++;

               string[j] = '0';

            }

      }

   }

   min = max = freq[0];

   for ( i = 0; i < freq.length; i++) {

      if (min > freq[i] && freq[i] != '0') {

         min = freq[i];

         minChar = string[i];

      }

       if (max < freq[i]) {

         max = freq[i];

         maxChar = string[i];

      }

   }

   System.out.println( "Minimum occurring character: " + minChar);

   System.out.println( "Maximum occurring character: " + maxChar);

    }

   }

output

Minimum occurring character: a Maximum occurring character: e

Theory

Q11_Program to determine whether one string is a rotation of another

Rotate

Program to determine whether one string is a rotation of another

In this program, we need to check whether a string is a rotation of another string or not.

Consider the above example, suppose we need to check whether string 2 is a rotation of string 1. To find this, we concatenate string 1 with string 1. Then, try to find the string 2 in concatenated string. If string 2 is present in concatenated string then, string 2 is rotation of string 1. String 2 deabc is found on the index 3 in concatenated string. So, deabc is rotation of abcde.

string 1

abcde

string 2

deabc

string 1 String2

abcdeabcde

Program

class one_string_rotation {

  public static void main(String[] args)   {

    //declare string

   String str1 = "abcde", str2 = "deabc"; ;

  if (str1.length() != str2.length())

   {

     System.out.println( "second string not rotate" );

   }

 else

  {

  str1=str1.concat(str1);

  if (str1.indexOf(str2)!=-1)

  {

    System.out.println( "second string rotate frist string" );

  }

else

{

  System.out.println( "second string rotate frist string" );

  }

    }

   }

output

Second string is a rotation of first string

Theory

in this program we have roate one string is roate to each other or not. we have to conact one string is itself then found the second string in concated frist string its that means a string is a rotation of another string. if firsr of all check lenght of both strings are equal then to chack wether a string is a rotation of another string . lenghts are not equals the stringits second string is not rotate first.

Q7 Program to remove all the white spaces from a string

remove_white_space

Program to remove all the white spaces from a string

In this program, our task is to remove all the white-spaces from the string. For this purpose, we need to traverse the string and check if any character of the string is matched with a white-space character or not. If so, use any built-in method like replace() with a blank.

In C, we do not have any built-in method to replace. Therefore, we need to run for loop to traverse the string and see if there is any white-space character or not. If so, then start the inner loop (j) from ith character to len and keep replacing each element with its next adjacent element. Decrease the length of the string by 1 on the termination of this loop. Repeat this process until all the white-spaces of the string are removed.

string 1

Remove white spaces

string 2

Removewhitespaces

Program

class remove_white_space {

  public static void main(String[] args)   {

    //declare string

   String str1="Remove white spaces";

  str=str.replaceAll("\\s+", "");

  System.out.println(str);

    }

   }

output

Removewhitespaces

Theory

Thursday, 2 December 2021

Program to find the longest repeating sequence in a string

Longest_repeating

Program to find the longest repeating sequence in a string

In this program, we need to find the substring which has been repeated in the original string more than once.

In the above string, the substring bdf is the longest sequence which has been repeated twice.

string 1

"acbdfghybdf"

Program

class Longest_repeating {

  public static String lcp(String s, String t){

   int n = Math.min(s.length(),t.length());

    for (int i = 0; i < n; i++){

    if (s.charAt(i) != t.charAt(i)){

     return s.substring(0,i);

     }

  }

   return s.substring(0,n);

 }

  public static void main(String[] args)   {

    //declare string

   String str1="acbdfghybdf";

  String lrs="";

  int n = str.length();

  for (int i = 0; i < n; i++){

   for (int j = i+1; j < n; j++){

  //Checks for the largest common factors in every substring

    String x = lcp(str.substring(i,n),str.substring(j,n));

     if (x.length() > lrs.length()) lrs=x;

   }

 }

   System.out.println( "Longest repeating sequence: " +lrs);

    }

   }

output

Longest repeating sequence: bdf

Theory

Java Program to determine whether a given string is palindrome

pallindrome

Java Program to determine whether a given string is palindrome

In this program, we need to check whether a given string is palindrome or not. A string is said to be palindrome if it is the same from both the ends. For e.g. above string is a palindrome because if we try to read it from backward, it is same as forward. One of the approach to check this is iterate through the string till middle of string and compare a character from back and forth.

string 1

"mama"

string 2

"kaka"

Program

class String_pallidrome {

  public static void main(String[] args)   {

    //declare string

   String str1="kaka";

  boolean flag= true;

  str= str.toLowerCase();

  for(int i=0;i< (str.length())/2;i++) {

  if (str.charAt(i) != str.charAt(str.length()-i-1))

  {

    flag=false

   break;

   }

  if(flag= true ){

     System.out.println( "pallidrome" );

  }

 else {

       System.out.println( "not pallidrome" );

     }

   }

   }

output

pallidrome

Theory

in this program , we need to check whether a string is palindrome or not. A string is said to palindrome then string backword write is some to write forword otherwise it is not a palindrome string

we are to travel loop to midle of the string to compare the first char to last char then increase the value of i an use flag is boolean varible declare is true but after travelling loop flag is rename true it is palindrome string as basis of our programming method other wise you are use various method as ssson possible to you.

How do you select data from a table in SQL?

Create Table How do you select data a table in SQL? The SELECT statement is used to se...