-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathString in java
More file actions
135 lines (128 loc) · 4.55 KB
/
Copy pathString in java
File metadata and controls
135 lines (128 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
1]write a program to remove duplicates from a string.
import java.util.LinkedHashSet;
import java.util.Set;
public class RemoveDuplicates {
public static String removeDuplicates(String input) {
Set<Character> set = new LinkedHashSet<>();
for (int i = 0; i < input.length(); i++) {
set.add(input.charAt(i));
}
StringBuilder sb = new StringBuilder();
for (Character character : set) {
sb.append(character);
}
return sb.toString();
}
public static void main(String[] args) {
String input = "geeksforgeeks";
System.out.println("Input string: " + input);
System.out.println("After removing duplicates: " + removeDuplicates(input));
}
}
=====================================================================================================================================================================
2] write a program to print duplicates characters from the string.
import java.util.HashMap;
import java.util.Map;
public class PrintDuplicates {
public static void printDuplicates(String input) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
System.out.println("Duplicate characters:");
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
if (entry.getValue() > 1) {
System.out.println(entry.getKey());
}
}
}
public static void main(String[] args) {
String input = "geeksforgeeks";
System.out.println("Input string: " + input);
printDuplicates(input);
}
}
======================================================================================================================================================================
3] write a program to check if “2552” is palindrome or not.
public class PalindromeCheck {
public static boolean isPalindrome(String input) {
int length = input.length();
for (int i = 0; i < length / 2; i++) {
if (input.charAt(i) != input.charAt(length - i - 1)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
String input = "2552";
if (isPalindrome(input)) {
System.out.println("The string " + input + " is a palindrome.");
} else {
System.out.println("The string " + input + " is not a palindrome.");
}
}
}
======================================================================================================================================================================
4] write a program to count the number of consonants, vowels,special characters in string.
public class CharacterCounter {
public static void countCharacters(String input) {
int vowels = 0;
int consonants = 0;
int special = 0;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
vowels++;
} else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
consonants++;
} else {
special++;
}
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
System.out.println("Special characters: " + special);
}
public static void main(String[] args) {
String input = "Hello, World!";
System.out.println("Input string: " + input);
countCharacters(input);
}
}
======================================================================================================================================================================
5]write a program to implement Anagram checking least in build method being used.
public class AnagramCheck {
public static boolean isAnagram(String input1, String input2) {
int length1 = input1.length();
int length2 = input2.length();
if (length1 != length2) {
return false;
}
int[] frequency = new int[26];
for (int i = 0; i < length1; i++) {
frequency[input1.charAt(i) - 'a']++;
frequency[input2.charAt(i) - 'a']--;
}
for (int count : frequency) {
if (count != 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
String input1 = "listen";
String input2 = "silent";
if (isAnagram(input1, input2)) {
System.out.println("The strings " + input1 + " and " + input2 + " are anagrams.");
} else {
System.out.println("The strings " + input1 + " and " + input2 + " are not anagrams.");
}
}
}