Friday, 3 December 2021

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.

Program to find all subsets of a string

pallindrome

Program to find all subsets of a string

In this program, all the subsets of the string need to be printed. The subset of a string is the character or the group of characters that are present inside the string. All the possible subsets for a string will be n(n+1)/2.

For example, all possible subsets of a string "FUN" will be F, U, N, FU, UN, FUN. To complete this program, we need to run two for loops. The outer loop is used to maintain the relative position of the first character and the second loop is used to create all possible subsets and prints them one by one.

string 1

"FUN"

Program

class Q5_subset {

  public static void main(String[] args)   {

    //declare string

   String str1="FUN";

   int len = str.length();

   int temp=0;

   //Total possible subsets for string of size n is n*(n+1)/2

   String arr[] = new String[len*(len+1)/2];

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

  //This loop adds the next character every iteration for the subset to form and add it to the array

  for for(int j = i; j < len; j++) {

  arr[temp] = str.substring(i, j+1);

  temp++;

   }

  }

  //This loop prints all the subsets formed from the string.

  System.out.println( "All subsets for given string are: " );

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

  System.out.println(arr[i]);

     }

    }

   }

output

All subsets for given string are: F FU FUN U UN N

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.

Wednesday, 1 December 2021

Java Program to replace lower-case characters with upper-case and vice-versa

uppercase_lowercase

Java Program to replace lower-case characters with upper-case and vice-versa

Here, our task is to replace all the lower-case characters in the string to upper-case and upper-case characters to lower-case.

For this purpose, we need to traverse the string and check for each character. If the character is a lower-case character, make it upper-case by using the language-specific built-in method or add 32 to the lower-case character in C to change the ASCII value of the character.

string 1

"This Is a Comman Name"

string 2

tHIS iS A cOMMAN nAME

Class

Program

class replace_upper_lower {

  public static void main(String[] args)   {

    //declare string

   String str1="This Is a Comman Name";

for (int i=0;i

//check for Lower case char

if (Character.isLowerCase(str.charAt(i)))

{

newstr.setCharAt(i,Character.toUpperCase(str.charAt(i)));

}

else

if(Character.isUpperCase(str.charAt(i)))

{

newstr.setCharAt(i,Character.toLowerCase(str.charAt(i)));

}

System.out.println(newstr);

   }

   }

output

tHIS iS A cOMMAN nAME

Theory

In the given program to task the given string char is convert uppercase char is lowercase and Lowercase is uppercase. it is main aim of the given program.

first of all , we can declare the any lenght of string . and one declare to StringBuffer is help to converting string are store that is orignal string is safe to after chake frist char is lowercase it is convert in Uppercase but its not store in orignal string in that resion we have require to StringBuffer. firstly we are chake each char is lowercase then the char is convert to uppercase to use toUpperCase() setCharAt() and charAt() method use in proper sequnce and is some mthod in Uppercase char. and finally print the string.

Java Program to replace the spaces of a string with a specific character

space

Java Program to replace the spaces of a string with a specific character

In this program, we need to replace all the spaces present in the string with a specific character.

One of the approach to accomplish this is by iterating through the string to find spaces. If spaces are present, then assign specific character in that index. Other approach is to use a built-in function replace function to replace space with a specific character.

string 1

"This Is a Comman Name"

string 2

"This-Is-a-Comman-Name"

Class

Program

class Replace_space_char {

  public static void main(String[] args)   {

    //declare string

     String str1="This Is a Comman Name";

     char ch = '-';

    str=str.replace(' ',ch);

    System.out.println(str);

    }

   }

output

This-Is-a-Comman-Name

Theory

In this program in main task in white space is replace by specific char in given string . fisrt of all declare the string and which char is replace is declare in ch variacle .

then we are used to replace() method to easy replace specific char by specific char is main use of replace() method in string. then finally print string to replace white space with specific char.

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...