Thursday, 2 December 2021

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.

Tuesday, 30 November 2021

Java Program to divide a string in 'N' equal parts

N_divide

Java Program to divide a string in 'N' equal parts.

Here, our task is to divide the string S into n equal parts. We will print an error message if the string cannot be divisible into n equal parts otherwise all the parts need to be printed as the output of the program.

To check whether the string can be divided into N equal parts, we need to divide the length of the string by n and assign the result to variable chars.

If the char comes out to be a floating point value, we can't divide the string otherwise run for loop to traverse the string and divide the string at every chars interval.

string 1

"aaaabbbbcccc"

Class

Program

class N_divide {

  public static void main(String[] args)   {

    //declare array

   String str1="Brag";

   int len = str.length();

   int n=3, temp=0, chars=len/n

   String[] equalStr = new String [n];

     //Check whether a string can be divided into n equal parts

   if (len % n != 0) {

       System.out.println("Sorry this string cannot be divided into " + n + " equal parts." ); }

   else {

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

            String part = str.substring(i, i+chars);

           equalStr[temp] = part;

             temp++;

         }

      }

      System.out.println(" equal parts of given string are " + n);

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

          System.out.println(equalStr[i]);
       }

   }

   }

output

3 equal parts of given string are
aaaa bbbb cccc

Theory

Monday, 29 November 2021

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

vowel

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

In this program, our task is to count the total number of vowels and consonants present in the given string. As we know that, the characters a, e, i, o, u are known as vowels in the English alphabet. Any character other than that is known as the consonant.

To solve this problem, First of all, we need to convert every upper-case character in the string to lower-case so that the comparisons can be done with the lower-case vowels only not upper-case vowels, i.e.(A, E, I, O, U). Then, we have to traverse the string using a for or while loop and match each character with all the vowels, i.e., a, e, i, o, u. If the match is found, increase the value of count by 1 otherwise continue with the normal flow of the program. The algorithm of the program is given below.

string

this is a comman name

Class

Program

class vowel {

public static void main(String[] args)

//declare String

String str1="this is a comman name";

int vCount = 0, Ccount = 0;

//Converting both the string to lower case.

str1 = str1.toLowerCase();

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

{

if(str.charAt(i)=='a' || str.charAt(i)=='e' || str.charAt(i)=='u' || str.charAt(i)=='o' || str.charAt(i)=='i' )

{

vcount++;

}

else if (str.charAt(i)>='a' && str.charAt(i) < ='z')

{

Ccount++;

}

System.out.println("total vowel is = "+ vcount);
System.out.println("constants is =" + Ccount);

}

}

output

total vowel is = 7
constants is =10

Theory:-

In this problem to firstly know what is vowels and constants .we have to five alphabate 'a' 'e' 'u' 'i' 'o' is the vowels . renaming alphabate are the constants expect black space and syball are not the constants.

To calculate of given string to how many vowels and constants are the present in string.

we are fristly to given string convert to either uppercase or lowercase .then use for loop to strating 0 and end to string length. in loop to specific word compair to vowels is use ChartAt() method in string. if condition is true then increase count of vowels. if condition is false then count constants of the given string.

Program to determine whether two strings are the anagram

Anagram

Program to determine whether two strings are the anagram

Two Strings are called the anagram if they contain the same characters. However, the order or sequence of the characters can be different. In this program, our task is to check for two strings that, they are the anagram or not. For this purpose, we are following a simpler approach.

First of all, Compare the length of the strings, if they are not equal in the length then print the error message and make an exit, otherwise, convert the string into lower-case for the easy comparisons. Sort both the strings using bubble sort or other sorting methods. If the strings are found to be identical after sorting, then print that strings are anagram otherwise print that strings are not the anagram.

string 1

Brag

String 2

Grab

Class

Program

class Anagram {

public static void main(String[] args)

//declare array

String str1="Brag";

String str2="Grab";

//Converting both the string to lower case.

str1 = str1.toLowerCase();

str2 = str2.toLowerCase();

//Checking for the length of strings

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

System.out.println( "Both the strings are not anagram" );

}

else{

//Converting both the arrays to character array

char[] string1 = str1.toCharArray();

char[] string2 = str2.toCharArray();

//Sorting the arrays using in-built function sort ()

Arrays.sort(string1);
Arrays.sort(string2);

//Comparing both the arrays using in-built function equals ()

if (Arrays.equals(string1, string2) == true ) {

System.out.println( "Both the strings are anagram" );

}

else{

System.out.println( "Both the strings are not anagram" );

}

}

output

Both the strings are anagram

Theory:-

the two string are called the anagram ,so that is some word/letter in the oder of sequnce is differnt. in this program taks to find the given two strings are anagram or not anagram.for this purpose the following method are given below:-

first of all, the given strings are to convert either uppercase or lowercase .then to find length of the given string by useing length() function of string. firstly we have to check the strings are equals length then to find out the anagram string but not length are equals then show out the error of the given program.

then , first of all string are converted into array .it is easy to sorting the string(array) as basis of the our requirement .in this problem we have to use Arrays sort() method use that is to sorted the given string. and then compaire two string are the true (quals).then print that strings are anagram otherwise that string are not anagram.

Sunday, 28 November 2021

Top 40 DAA Interview Questions (2021)

oops/c++

DAA Interview Questions

The name 'Algorithm' refers to the sequence of instruction that must be followed to clarify a problem.
The logical description of the instructions which may be executed to perform an essential function.

The time complexity of an algorithm denoted the total time needed by the program to run to completion. It is generally expressed by using the big O notation.

Any subset that satisfies these constraints is known as a feasible solution. A feasible solution, which either maximizes or minimizes a given purpose method is known as an optimal solution.

DP is another method for problems with optimal substructure: An optimal solution to a problem include optimal solutions to subproblems. This doesn't necessarily define that every optimal solution to a subproblem will contribute to the primary solution.

For divide and conquer (top-down), the subproblems are independent, so we can resolve them in any order.

Warshall's algorithm is a function of dynamic programming procedure, which is used to find the transitive closure of a directed graph.

A greedy technique for an optimization problem always makes the option which look best at the moment and add it to the current subsolution.

1.The greedy method produces a feasible solution

2.The greedy method is very easy to solve a problem

3.The greedy method implements an optimal solution directly

A spanning tree for a linked graph is a tree whose vertex set is the same as the vertex set of the given graph, and whose edge set is a subgroup of the edge set of the given graph. i.e., any linked graph will have a spanning tree.

This is a greedy method. A greedy method chooses some local optimum (i.e., selection an edge with the smallest weight in an MST).

Floyd's algorithm is a function, which is used to find all the pairs shortest paths problem. Floyd's algorithm is relevant to both directed and undirected weighted graph, but they do not include a cycle of a negative length.

Prim's algorithm is a greedy and efficient technique, which is used to find the minimum spanning the tree of a weighted linked graph.

Dijkstra's algorithm solves the single-source shortest path method of finding shortest paths from a given vertex (the source), to another vertex of a weighted graph or digraph. Dijkstra's algorithm implements a correct solution for a graph with non-negative weights.

A Huffman tree is a binary tree which reduces the weighted path length from the root to the leaves, including a set of predefined weights. The essential application of Huffman trees is a Huffman code.

A Huffman code is an optimal prefix tree variable-length encoding technique which assign bit strings to characters based on their frequency in a given text.

Depth-first node generation with bounding method is known as backtracking. The backtracking technique has its virtue the ability to yield the solution with far fewer than m trials.

Dynamic programming Greedy method
Many numbers of decisions are generated. Only one sequence of decision is generated.
It gives an optimal solution always It does not require to provide an optimal solution always.

The problem is to area n-queens on an n-by-n chessboard so that no two queens charge each other by being same row or in the same column or the same diagonal.

The types of Notations used for Time Complexity includes:-

Big Oh:It indicates “fewer than or the same as” < expression >iterations

Big Omega:It indicates “more than or same as” < expression >iterations

Big Theta:It indicates “the same as”< expression >iterations

Recursive algorithm is a method of solving a complicated problem by breaking a problem down into smaller and smaller sub-problems until you get the problem small enough that it can be solved easily. Usually, it involves a function calling itself.

It is a language feature that allows a subclass or child class to provide a specific implementation of a method which is already provided by one of its super classes or parent classes.

The constructor has the same name as the class. A constructor is also a special kind of method. It is used to initialize objects of the class.

Access modifiers or access specifiers are the keywords in object-oriented languages. It helps to set the accessibility of classes, methods, and other members.

If you derive a class from another class that is known as inheritance. The child class will inherit all the public and protected properties and methods from the parent class. The child class can also have its own properties and methods. An inherited class is defined by using the extends keyword.

combination of multiple and multi-level inheritance is known as hybrid inheritance.

Hierarchical inheritance is basically when one base class has more than one subclasses. For example, the fruit class can have ‘apple’, ’mango’, ’banana’, ‘cherry’ etc. as its subclasses.

It Increases the execution time and effort. It also requires jumping back and forth between different classes. The parent class and the child class is always tightly coupled.

Afford modifications in the program would require changes for parent and the child class both. Inheritance requires careful implementation otherwise it would lead to incorrect results.

A superclass or base class is also a class which works as a parent to some other class/ classes.

For example, the Vehicle class is a superclass of class Bike.

A subclass is a class that inherits from another class.

For example, the class Bike is a subclass or a derived of Vehicle class.

Yes, the constructors can be overloaded by changing the number of arguments accepted by the constructor or by changing the data type of the parameters

Static polymorphism or static binding is a one kind of polymorphism which comes at compile time. example of compile-time polymorphism is: method overloading.

Dynamic polymorphism, dynamic binding or Runtime polymorphism is also part of polymorphism which is basically solved during runtime. An example of runtime polymorphism: method overriding.

Operator overloading is used to implement operators using user-defined types, based on the arguments passed along with it.

Data abstraction is one of the most important features of OOPs. It only allows important information to be displayed. It helps to hide the implementation details.

For example, while using a mobile, you know, how can you message or call someone but you don’t know how it actually happens.

This is data abstraction as the implementation details are hidden from the user.

Data abstraction can be achieved using two ways:

1. Abstract class
2. Abstract method

An abstract class is also a class which is consists of abstract methods.

These methods are basically declared but not defined and If these methods need to be used later in some subclass that time those methods haveto be exclusively defined in the subclass.

Virtual functions are also part of the functions which are present in the parent class and they are overridden by the subclass.
These functions help to achieve runtime polymorphism.

Garbage Collection is a part of automatic memory management. The Garbage collector helps to free the occupied spaces by objects. Those spaces are no longer in existence.

Zero instances will be created for an abstract class. In other words, you cannot create an instance of an Abstract Class.

An exception is a kind of message that interrupts and comes up when there is an issue with normal execution of a program. Exceptions provide a error and transfer that error to the exception handler to resolve it.

Exception handling in Object-Oriented Programming is the most important concept. It is used to manage errors. An exception handler help to throw errors and then catch the error in order to solve them.

A try/ catch block helps to handle exceptions. The try block explains a set of statements that may have an error. The catch block basically catches the exception.

An inline function is a technique used by the compilers and instructs to insert complete body of the function wherever that function is used in the program source code.

A friend function is a friend of a class that is allowed to access to Public, private, or protected data in that same class. If the function is defined outside the class cannot access such information.

The super keyword is used to invoke the overridden method, which overrides one of its superclass methods. This keyword allows to access overridden methods and also to access hidden members of the superclass. It also forwards a call from a constructor, to a constructor in the superclass.

Operator keyword is used for overloading.

An interface is a collection of an abstract method. If the class implements an interface, it thereby inherits all the abstract methods of an interface. Java uses Interface to implement multiple inheritances.

Early binding refers to the assignment of values to variables during design time, whereas late Binding refers to the assignment of values to variables during run time.

A pure virtual function is a function which can be overridden in the derived class but cannot be defined. A virtual function can be declared as Pure by using the operator =0.

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