0% found this document useful (0 votes)
82 views15 pages

Public Class Charrep (

1. The removeDuplicates method removes duplicate characters from a character array by iterating through the array, comparing each character to the previous, and only adding it to the new string if it is different. It returns the de-duplicated string. 2. The main method checks if a number is a palindrome by reversing the number, converting it to an integer, and comparing it to the original number. 3. The firstNonRepeatedCharacter method sorts the characters of a string, then iterates through to find the first character that is not repeated immediately before or after in the sorted string.

Uploaded by

Murty SVVSN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views15 pages

Public Class Charrep (

1. The removeDuplicates method removes duplicate characters from a character array by iterating through the array, comparing each character to the previous, and only adding it to the new string if it is different. It returns the de-duplicated string. 2. The main method checks if a number is a palindrome by reversing the number, converting it to an integer, and comparing it to the original number. 3. The firstNonRepeatedCharacter method sorts the characters of a string, then iterates through to find the first character that is not repeated immediately before or after in the sorted string.

Uploaded by

Murty SVVSN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1)

public class CharRep {

public static String removeDuplicates(char[] chars)

char prev = '\0';

int k = 0;

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

if (prev != chars[i]) {

chars[k++] = chars[i];

prev = chars[i];

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

return new String(chars).substring(0, k);

public static void main(String[] args)

String s = "AAABBCDDD";

s = removeDuplicates(s.toCharArray());

System.out.println(s);

}
2) public class Palindrome {

public static void main(String[] args) {

int num = 121, reverseVal = 0, rem, originalVal;

originalVal = num;

while( num != 0 )
{
rem = num % 10;
reverseVal = reverseVal * 10 + rem;
num /= 10;
}

if (originalVal == reverseVal)
System.out.println(originalVal + " is a palindrome.");
else
System.out.println(originalVal + " is not a palindrome.");
}
}

3)

public class FirstNonRepeatedCharacter {

public static void main(String[] args) {

String source = "ffeeddbbaaclck";

char[] chars = source.toCharArray();

Arrays.sort(chars);

String sorted = String.valueOf(chars);

char ch = 0;

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

if (i == 0) {

if (sorted.charAt(i) != sorted.charAt(i + 1)) {

ch = sorted.charAt(i);

} else {

if (i == sorted.length() - 1) {

if (sorted.charAt(i) != sorted.charAt(i - 1)) {


ch = sorted.charAt(i);

} else {

if (sorted.charAt(i) != sorted.charAt(i + 1)

&& sorted.charAt(i) != sorted.charAt(i - 1)) {

ch = sorted.charAt(i);

System.out.println("First non repeated character in String \"" + source +

"\" :" + ch);

Output:

First non repeated characted in String "ffeeddbbaaclck" is:l

4)
public class ReverseString
{
static int i,c=0,res;

static void stringreverse(String s)


{
char ch[]=new char[s.length()];
for(i=0;i < s.length();i++)
ch[i]=s.charAt(i);
for(i=s.length()-1;i>=0;i--)
System.out.print(ch[i]);
}

public static void main (String args[])


{
System.out.println("Original String is : ");
System.out.println("Java Program");
CharRep.stringreverse(" Java Program ");
}
}

5)

// Java Program to illustrate Pangram

class GFG

// Returns true if the string

// is pangram else false

public static boolean checkPangram (String str)

// Create a hash table to mark the

// characters present in the string

// By default all the elements of

// mark would be false.

boolean[] mark = new boolean[26];

// For indexing in mark[]

int index = 0;

// Traverse all characters

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

// If uppercase character, subtract 'A'

// to find index.

if ('A' <= str.charAt(i) &&

str.charAt(i) <= 'Z')

index = str.charAt(i) - 'A';


// If lowercase character, subtract 'a'

// to find index.

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

str.charAt(i) <= 'z')

index = str.charAt(i) - 'a';

// Mark current character

mark[index] = true;

// Return false if any character is unmarked

for (int i = 0; i <= 25; i++)

if (mark[i] == false)

return (false);

// If all characters were present

return (true);

// Driver Code

public static void main(String[] args)

String str = "The quick brown fox jumps over the lazy dog";

if (checkPangram(str) == true)

System.out.print(str + " is a pangram.");

else
System.out.print(str+ " is not a pangram.");

6)
import java.util.Scanner;

public class CharRep {

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);

System.out.println("Enter the string : ");


String input = in.nextLine();

String out = checkPangram(input);

if (out.length() == 0)
System.out.println("Pangram");
else
System.out.println("The letters missing : " + out);
}

private static String checkPangram(String input)


{
input = input.toLowerCase();
String alph = "abcdefghijklmnopqrstuvwxyz";
int max = 26;

for (int i = 0; i < max; i++)


{
int index = input.indexOf(alph.charAt(i));
if (index != -1)
{
alph = alph.substring(0, i) + alph.substring(i + 1);
i--;
max--;
}
}

return alph;
}

7) class nthFibonacci {
static int fib(int n)
{
/* Declare an array to store Fibonacci numbers. */
int f[] = new int[n + 1];
int i;

/* 0th and 1st number of the series are 0 and 1*/


f[0] = 0;
f[1] = 1;

for (i = 2; i <= n; i++) {


/* Add the previous 2 numbers in the series
and store it */
f[i] = f[i - 1] + f[i - 2];
}

return f[n];
}

public static void main(String args[])


{
int n = 1;
System.out.println(fib(n));
}
}

8) // Java Program to swap two numbers without

// using temporary variable

import java.*;

class Geeks {

public static void main(String a[])

int x = 10;

int y = 5;

x = x + y;

y = x - y;

x = x - y;

System.out.println("After swaping:"

+ " x = " + x + ", y = " + y);


}

9)

public class Prime {

public static void main(String[] args) {

int num = 29;

boolean flag = false;

for(int i = 2; i <= num/2; ++i)

// condition for nonprime number

if(num % i == 0)

flag = true;

break;

if (!flag)

System.out.println(num + " is a prime number.");

else

System.out.println(num + " is not a prime number.");

}
10)
public class Armstrong {

public static void main(String[] args) {

int number = 371, originalNumber, remainder, result = 0;

originalNumber = number;

while (originalNumber != 0)

remainder = originalNumber % 10;

result += Math.pow(remainder, 3);

originalNumber /= 10;

if(result == number)

System.out.println(number + " is an Armstrong number.");

else

System.out.println(number + " is not an Armstrong number.");

11)

// Java program to check if a number is power of

// another number

public class Test {


public static void main(String[] args)

// check the result for true/false and print.

System.out.println(isPower(10, 1) ? 1 : 0);

System.out.println(isPower(1, 20) ? 1 : 0);

System.out.println(isPower(2, 128) ? 1 : 0);

System.out.println(isPower(2, 30) ? 1 : 0);

/* Returns true if y is a power of x */

public static boolean isPower(int x, int y)

// The only power of 1 is 1 itself

if (x == 1)

return (y == 1);

// Repeatedly compute power of x

int pow = 1;

while (pow < y)

pow = pow * x;

// Check if power of x becomes y

return (pow == y);

12)

// Java program to find Minimum Distance

// Between Words of a String

class solution
{

// Function to calculate the minimum

// distance between w1 and w2 in s

static int distance(String s,String w1,String w2)

if (w1 .equals( w2) )

return 0 ;

// get individual words in a list

String words[] = s.split(" ");

// assume total length of the string

// as minimum distance

int min_dist = (words.length) + 1;

// traverse through the entire string

for (int index = 0;

index < words.length ; index ++)

if (words[index] .equals( w1))

for (int search = 0;

search < words.length; search ++)

if (words[search] .equals(w2))

{
// the distance between the words is

// the index of the first word - the

// current word index

int curr = Math.abs(index - search) - 1;

// comparing current distance with

// the previously assumed distance

if (curr < min_dist)

min_dist = curr ;

// w1 and w2 are same and adjacent

return min_dist;

// Driver code

public static void main(String args[])

String s = "MURTY SUNKARA : surname is SUNKARA in full name;

String w1 = "SUNKARA" ;

String w2 = "name" ;

System.out.print( distance(s, w1, w2) );


}

13)
// Java program to remove duplicates from a sorted linked list
class LinkedList
{
Node head; // head of list

/* Linked list Node*/


class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}

void removeDuplicates()
{
/*Another reference to head*/
Node current = head;

/* Pointer to store the next pointer of a node to be deleted*/


Node next_next;

/* do nothing if the list is empty */


if (head == null)
return;

/* Traverse list till the last node */


while (current.next != null) {

/*Compare current node with the next node */


if (current.data == current.next.data) {
next_next = current.next.next;
current.next = null;
current.next = next_next;
}
else // advance if no deletion
current = current.next;
}
}

/* Utility functions */

/* Inserts a new Node at front of the list. */


public void push(int new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);

/* 3. Make next of new Node as head */


new_node.next = head;

/* 4. Move the head to point to new Node */


head = new_node;
}

/* Function to print linked list */


void printList()
{
Node temp = head;
while (temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}

/* Drier program to test above functions */


public static void main(String args[])
{
LinkedList llist = new LinkedList();
llist.push(20);
llist.push(13);
llist.push(13);
llist.push(11);
llist.push(11);
llist.push(11);

System.out.println("List before removal of duplicates");


llist.printList();

llist.removeDuplicates();

System.out.println("List after removal of elements");


llist.printList();
}
}

14)

// A Java program to find floor(sqrt(x))

class GFG {

// Returns floor of square root of x


static int floorSqrt(int x)

// Base cases

if (x == 0 || x == 1)

return x;

// Staring from 1, try all numbers until

// i*i is greater than or equal to x.

int i = 1, result = 1;

while (result <= x) {

i++;

result = i * i;

return i - 1;

// Driver program

public static void main(String[] args)

int x = 11;

System.out.print(floorSqrt(x));

You might also like