0% found this document useful (0 votes)
4 views12 pages

String

The document contains multiple Java programs that demonstrate various string manipulation techniques, including counting characters, reversing strings, checking for palindromes, sorting characters, and finding duplicates. Each program is encapsulated in a separate class within the 'Strings' package, showcasing different functionalities such as counting vowels and consonants, reversing sentences, and identifying the longest word in a sentence. The examples illustrate fundamental programming concepts and string operations in Java.

Uploaded by

SREENIVAS GV
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)
4 views12 pages

String

The document contains multiple Java programs that demonstrate various string manipulation techniques, including counting characters, reversing strings, checking for palindromes, sorting characters, and finding duplicates. Each program is encapsulated in a separate class within the 'Strings' package, showcasing different functionalities such as counting vowels and consonants, reversing sentences, and identifying the longest word in a sentence. The examples illustrate fundamental programming concepts and string operations in Java.

Uploaded by

SREENIVAS GV
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/ 12

String

package Strings;

/* wap to count the number of characters in a String*/

public class p1 {

public static void main(String[] args) {


String s = "Java Is Easy";
int countChar = 0;

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


if (s.charAt(i) != ' ') {
countChar++;
}
}
System.out.println(countChar);
}

package Strings;

/* wap to reverse a String */

public class p2 {

public static void main(String[] args) {


String s = "Disha";
String reverse = "";
for (int i = s.length() - 1; i >= 0; i--) {
reverse = reverse + s.charAt(i);
}
System.out.println(reverse);
}
}

package Strings;

/* wap to check whether the String is palindrom */

public class p3 {

public static void main(String[] args) {


String s = "malayalam";
String reverse = "";
for (int i = s.length() - 1; i >= 0; i--) {
reverse = reverse + s.charAt(i);
}
if (reverse.contentEquals(s)) {
System.out.println("It is a Palindrome");
} else {
System.out.println("It is not a Palindrom");
}
}
}
package Strings;

/* wap to sort the characters in a string */

public class p4 {

public static void bubbleSort(char[] a) {


for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
char temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}

public static void main(String[] args) {


String s = "deepika";
char[] ch = s.toCharArray();
bubbleSort(ch);
String x = new String(ch);
System.out.println(x);
}
}

package Strings;
/* wap to reverse a sentence */

public class p5 {
public static void main(String[] args) {
String s = "Abhi Is Handsome ";
String reverse = "";
String[] ch = s.split(" ");
for (int i = ch.length - 1; i >= 0; i--) {
reverse += ch[i] + " ";
}
System.out.println(reverse);
}
}

package Strings;

/* wap to count the number of words in a sentence*/

public class p6 {

public static void main(String[] args) {


String s = "Do not retaliate Trup hits China with traiffs";
String[] ch = s.split(" ");
System.out.println(ch.length);
}
}

package Strings;
/* wap to find the longest word in a sentence */

public class p7 {

public static void main(String[] args) {


String s = "Arnold is the Best BodyBuilder";
String[] ch = s.split(" ");
String longest = " ";
for (int i = 0; i < ch.length; i++) {
if (ch[i].length() > longest.length()) {
longest = ch[i];
}
}
System.out.println(longest);
}
}

package Strings;

/* wap to swap two strings without using temporary variables */

public class p8 {

public static void main(String[] args) {


String s1 = "Disha";
String s2 = "Patani";
System.out.println(s1);
System.out.println(s2);
System.out.println("-------------------------------------");
s1 = s1 + s2;
s2 = s1.substring(0, s1.length() - s2.length());
s1 = s1.substring(s2.length());
System.out.println(s1);
System.out.println(s2);
}
}

package Strings;

/* wap to find the length of the string without using inbuilt length() */

public class p9 {

public static void main(String[] args) {


String s = "Deepika";
char[] ch = s.toCharArray();
int length = 0;
/* for each loop
* for(char lv : ch)
*/
for (int i = 0; i < ch.length; i++) {
length++;
}
System.out.println(length);
}
}

package Strings;
/* wap to count the number of vowels, consonents and digits */

public class p10 {

public static void main(String[] args) {


String s = "deepika is 5 10 tall and beautiful";
char[] ch = s.toCharArray();
int vowelCount = 0;
int consonentCount = 0;
int digitCount = 0;
for (char lv : ch) {
if (lv == 'a' || lv == 'e' || lv == 'i' || lv == 'o' || lv == 'u') {
vowelCount++;
} else if (lv >= 'a' && lv <= 'z') {
consonentCount++;
} else if (lv >= '0' && lv <= '9') {
digitCount++;
}
}
System.out.println(vowelCount);
System.out.println(consonentCount);
System.out.println(digitCount);
}
}

package Strings;

/* wap to reverse the string using method recurssion */


public class p11 {

public static String reverse(String s) {


if (s.length() == 1)
return s;
else
return s.charAt(s.length() - 1) + reverse(s.substring(0, s.length() - 1));
}

public static void main(String[] args) {


System.out.println(reverse("java"));
}
}

package Strings;

import java.util.Arrays;

/* wap to find the duplicate or repeated characters in a string */

public class p12 {

public static void main(String[] args) {


String s = "deepika padu";
char[] ch = s.toCharArray();
Arrays.sort(ch);
for (int i = 0; i < ch.length - 1; i++) {
if (ch[i] == ch[i + 1]) {
System.out.println(ch[i + 1]);
}
}
}
}

package Strings;

/* wap to find the duplicate or repeated characters in a string */

public class p13 {

public static void main(String[] args) {


String s = "deepika padu";
char[] ch = s.toCharArray();
for (int i = ch.length - 1; i >= 0; i--) {
boolean isDulicate = false;
for (int j = i - 1; j >= 0; j--) {
if (ch[j] == ch[i]) {
isDulicate = true;
break;
}
}
if (isDulicate) {
System.out.println(ch[i]);
}
}
}
}

package Strings;

import java.util.Arrays;

/* wap to print non duplicate characters in a string */

public class p14 {


public static void main(String[] args) {
String s = "deepika padu";
char[] ch = s.toCharArray();
Arrays.sort(ch);
for (int i = 0; i < ch.length - 1; i++) {
if (ch[i] != ch[i + 1]) {
System.out.println(ch[i]);
}
}
System.out.println(ch.length-1);
}
}

package Strings;

import java.util.LinkedHashSet;

/* wap to find the duplicate or repeated characters in a string using collection */

public class p15 {


public static void main(String[] args) {
String s = "deepika padkone";

LinkedHashSet<Character> l = new LinkedHashSet<Character>();

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


if (!l.add(s.charAt(i))) {
System.out.println(s.charAt(i));
}
}
}
}

package Strings;

import java.util.LinkedHashSet;

/* wap to print non repeated characters in a string using collection */

public class p16 {

public static void main(String[] args) {


String s = "deepika padukone";

LinkedHashSet<Character> l = new LinkedHashSet<Character>();


for (int i = 0; i < s.length(); i++) {
if (l.add(s.charAt(i))) {
System.out.println(s.charAt(i));
}
}
}
}

You might also like