0% found this document useful (0 votes)
4 views

String_Logical

The document contains various Java programming exercises focused on string manipulation, including reversing strings, counting words, finding duplicates, and identifying palindromes. It provides code snippets and explanations for each task, demonstrating different methods to achieve the desired outcomes. Additionally, it covers removing duplicates from arrays and lists, as well as counting vowels and consonants in a string.

Uploaded by

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

String_Logical

The document contains various Java programming exercises focused on string manipulation, including reversing strings, counting words, finding duplicates, and identifying palindromes. It provides code snippets and explanations for each task, demonstrating different methods to achieve the desired outcomes. Additionally, it covers removing duplicates from arrays and lists, as well as counting vowels and consonants in a string.

Uploaded by

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

Q1 Reverse String

-----------------
ans : ip : Badri Lal";
op: rehcoG laL
String rev =" ";

String str="Badri Lal Gocher";


for(int i=str.length()-1; i>=0; i--)
{
rev=rev+str.charAt(i);
}

System.out.println(rev);
}

---------------------------------------------

Q 2- WORD Reverse in given string ?


ans:
Badri Lal Gocher
irdaB laL rehcoG

public String getWordReverse(String str)


{
String[] words=str.split(" ");
String revstring="";

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


{
String word = words[i];
String revword="";
for(int j=word.length()-1; j>=0; j--)
{
revword=revword+word.charAt(j);
}

revstring = revstring+revword+" ";


}
return revstring;

}
------------------------------------------------

Q3- Total WORD Count in Given string ?

------------------------------------------------
ans: ip : "Badri Lal Gocher";
op: Count :3

public static void main(String[] args)


{
String str="Badri Lal Gocher";
int count=1;
for(int i=0; i<str.length()-1; i++)
{
if((str.charAt(i)== ' ')&&( str.charAt(i+1)!= ' '))
{
count++;
}
}
System.out.println("Count :"+count);

or in short
------------
String[] words = s.trim().split(" ");
System.out.println("Number of words in the string = "+words.length);

------------------------------------------------------------------------

Q3- Find duplicate WORD from Single String Array

ip: arr={"Java","Python","C","Java","PHP"};
op : Duplicate Element : Java

sol :

public static void main(String[] args)


{
CountChar obj = new CountChar();

String [] arr={"Java","Python","PHP","Java","PHP"};

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


{
for(int j=0; j<arr.length; j++)
{
if((arr[i].equals(arr[j]))&&(i!=j))
{
System.out.println("Duplicate Element : "+arr[j]);
}
}
}
}

-------------------------------------------

Q-4 find duplicate WORD from 2 String array?


ans:
String[] arr1 = {"ONE", "TWO", "THREE", "FOUR", "FIVE", "FOUR"};
String[] arr2 = {"THREE", "FOUR", "FIVE", "SIX", "SEVEN", "FOUR"};
Sol:
Duplicates :[FIVE, FOUR, THREE]

public static void main(String[] args)


{
CountChar obj = new CountChar();

String[] arr1 = {"ONE", "TWO", "THREE", "FOUR", "FIVE", "FOUR"};

String[] arr2 = {"THREE", "FOUR", "FIVE", "SIX", "SEVEN", "FOUR"};


HashSet<String> set = new HashSet<String>();

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


{
for(int j=0; j<arr2.length;j++)
{
if(arr1[i].equals(arr2[j]))
{
set.add(arr1[i]);
}
}
}

System.out.println("Duplicates :"+set);
}

---------------------------------------------------------------
Q- Compare Char or Stirng array
ans
char[] arr1={'A','B','C'};
char[] arr2={'A','B','C'};
or
String[] arr1={"JAVA","PHP","PYTHON","C"};
String[] arr2={"JAVA","PHP","C"};

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


{
for(int j=0; j<arr2.length;j++)
{
if(arr1[i] == arr2[j])
{
System.out.println(arr1[i]+" : is valid");
}

}
}
}

-------------------------------------------

Q 5- Remove Duplicate words from given String


ans:
ip: java and java spring with hibernet
op: java and spring with hibernet

public void removeDuplicate(String str)


{
String[] words=str.split(" ");

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


{
if(words[i] != null)
{
String word=words[i];
for(int j=i+1; j<words.length; j++)
{
if(words[i].equals(words[j]))
{
words[j]=null;
}
}
}
}

for(int k=0; k<words.length ; k++)


{
if(words[k] != null)
{
System.out.print(words[k]+" ");
}
}

-----------------------------------------

Q 6- REVERSE STRING VALUES WITHOUT 3RD VARIABLE


ANS:
String s1 = "ABC";
String s2 = "DEF";

s1=s1.concat(s2);
s2=s1.substring(0,s1.length()-s2.length());
s1=s1.substring(s2.length());

System.out.println(s1 +" : "+s2);

DEF : ABC

--------------------------------

Q 8 - find the occurance of WORD in given String


ip : Courses ="java php python java php";
OP:
python:1
java:2
php:2

public void getDuplicate(String str)


{
String[] arr =str.split(" ");

HashMap<String, Integer> hm = new HashMap<String, Integer>();

for(String strname :arr)


{
if(hm.containsKey(strname))
{
hm.put(strname, hm.get(strname)+1);
}
else
{
hm.put(strname, 1);
}
}

Set<String> set =hm.keySet();

for(String names : set)


{ //if only duplicate print then use
//if(hm.get(names)>1)
System.out.println(names +":"+hm.get(names));
}
}

------------------------------

Q 9 - FIND STRING IS PALINDROME OR NOT ?


ANS: //MADAM
public static void main(String[] args)
{

CountChar obj = new CountChar();

System.out.println("Please Enter String ");


Scanner sc = new Scanner(System.in);
String str=sc.next();
CountChar.getPalindrome(str);

public static void getPalindrome(String str)


{
String reverse ="";

for(int i=str.length()-1 ; i>=0 ; i--)


{
reverse=reverse+str.charAt(i);
}
if(str.equalsIgnoreCase(reverse))
{
System.out.println("String is Palindrom : "+str);
}
else{
System.out.println("Not Palindrome : "+str);
}
}

OR USING - STRING BUFFER


public static boolean isPalindrome(String input) {
if (input == null || input.isEmpty()) {
return true;
}

char[] array = input.toCharArray();


StringBuilder sb = new StringBuilder(input.length());
for (int i = input.length() - 1; i >= 0; i--) {
sb.append(array[i]);
}

String reverseOfString = sb.toString();

return input.equals(reverseOfString);
}

------------------------------------------------------------------
Q 10 - find Vowels and Consonants in string
Ans:
Vowel : 6
Consonant : 11
public static void countVowel(String str)
{
int len=str.length();
char[] arr =str.toCharArray();
int count = 0;

for(Character ch :arr)
{
if(ch=='a' ||ch=='e' || ch=='i' ||ch=='o'|| ch=='u')
{
count++;
}
}
System.out.println("Vowel :"+count);
System.out.println("Consonant :"+(len-count));

-------------------------------

Q-11 Remove First and Last Char from String


ans:
in : "Badri Lal";
op : adri La

String str="Badri Lal";


str=str.substring(1, str.length()-1);
System.out.println(str);

----------------------------

Q-How to remove repeated/duplicate elements from ArrayList ?


==============================================================
1 : whithout using collection
-------------------------------
package com.intellect;

import java.util.ArrayList;
public class MyHashMap {

public static void main(String[] args) {

ArrayList<Object> al = new ArrayList<Object>();


al.add("BADRI LAL");
al.add("Monika");
al.add("Nagendra");
al.add("BADRI LAL");
al.add("101");
al.add("JAVA");
al.add("101");
al.add(101);

System.out.println("Befor duplicate :"+al);


for(int i=0; i<al.size(); i++){

for(int j=i+1; j<al.size(); j++)


{
if(al.get(i).equals(al.get(j)))
{
al.remove(j);
j--;
}
}
}
System.out.println("after removed duplicate :"+al);
}

Remove using collection object HashSet :


========================================

package com.intellect;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class MyHashMap {

public static void main(String[] args) {

ArrayList<String> al = new ArrayList<String>();


al.add("BADRI LAL");
al.add("Monika");
al.add("Nagendra");
al.add("BADRI LAL");
al.add("101");
al.add("JAVA");
al.add("101");
System.out.println("al :"+al);

HashSet<String> hashset = new HashSet<String>(al);


System.out.println("hashset"+hashset);

al.clear();
al.addAll(hashset);
System.out.println("al"+al);

You might also like