File tree Expand file tree Collapse file tree 5 files changed +160
-0
lines changed
Expand file tree Collapse file tree 5 files changed +160
-0
lines changed Original file line number Diff line number Diff line change 1+ package strings ;
2+
3+ public class CharactersSame {
4+
5+ /**
6+ * Driver Code
7+ */
8+ public static void main (String [] args ) {
9+ assert isAllCharactersSame ("" );
10+ assert !isAllCharactersSame ("aab" );
11+ assert isAllCharactersSame ("aaa" );
12+ assert isAllCharactersSame ("11111" );
13+ }
14+
15+ /**
16+ * check if all the characters of a string are same
17+ *
18+ * @param s the string to check
19+ * @return {@code true} if all characters of a string are same, otherwise {@code false}
20+ */
21+ public static boolean isAllCharactersSame (String s ) {
22+ for (int i = 1 , length = s .length (); i < length ; ++i ) {
23+ if (s .charAt (i ) != s .charAt (0 )) {
24+ return false ;
25+ }
26+ }
27+ return true ;
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package strings ;
2+
3+ import java .util .Arrays ;
4+
5+ /**
6+ * Two strings are anagrams if they are made of the same letters
7+ * arranged differently (ignoring the case).
8+ */
9+ public class CheckAnagrams {
10+ public static void main (String [] args ) {
11+ assert isAnagrams ("Silent" , "Listen" );
12+ assert isAnagrams ("This is a string" , "Is this a string" );
13+ assert !isAnagrams ("There" , "Their" );
14+ }
15+
16+ /**
17+ * Check if two strings are anagrams or not
18+ *
19+ * @param s1 the first string
20+ * @param s2 the second string
21+ * @return {@code true} if two string are anagrams, otherwise {@code false}
22+ */
23+ public static boolean isAnagrams (String s1 , String s2 ) {
24+ s1 = s1 .toLowerCase ();
25+ s2 = s2 .toLowerCase ();
26+ char [] values1 = s1 .toCharArray ();
27+ char [] values2 = s2 .toCharArray ();
28+ Arrays .sort (values1 );
29+ Arrays .sort (values2 );
30+ return new String (values1 ).equals (new String (values2 ));
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ package strings ;
2+
3+ public class Lower {
4+
5+ /**
6+ * Driver Code
7+ */
8+ public static void main (String [] args ) {
9+ String [] strings = {"ABC" , "ABC123" , "abcABC" , "abc123ABC" };
10+ for (String s : strings ) {
11+ assert toLowerCase (s ).equals (s .toLowerCase ());
12+ }
13+ }
14+
15+ /**
16+ * Converts all of the characters in this {@code String} to lower case
17+ *
18+ * @param s the string to convert
19+ * @return the {@code String}, converted to lowercase.
20+ */
21+ public static String toLowerCase (String s ) {
22+ char [] values = s .toCharArray ();
23+ for (int i = 0 ; i < values .length ; ++i ) {
24+ if (Character .isLetter (values [i ]) && Character .isUpperCase (values [i ])) {
25+ values [i ] = Character .toLowerCase (values [i ]);
26+ }
27+ }
28+ return new String (values );
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ package strings ;
2+
3+ /**
4+ * Wikipedia: https://en.wikipedia.org/wiki/Pangram
5+ */
6+ public class Pangram {
7+
8+ /**
9+ * Driver Code
10+ */
11+ public static void main (String [] args ) {
12+ assert isPangram ("The quick brown fox jumps over the lazy dog" );
13+ assert !isPangram ("The quick brown fox jumps over the azy dog" ); /* not exists l character */
14+ }
15+
16+ /**
17+ * Check if a string is a pangram string or not
18+ *
19+ * @param s string to check
20+ * @return {@code true} if given string is pangram, otherwise {@code false}
21+ */
22+ public static boolean isPangram (String s ) {
23+ boolean [] marked = new boolean [26 ]; /* by default all letters don't exists */
24+ char [] values = s .toCharArray ();
25+ for (char value : values ) {
26+ if (Character .isLetter (value )) {
27+ int index = Character .isUpperCase (value ) ? value - 'A' : value - 'a' ;
28+ marked [index ] = true ; /* mark current character exists */
29+ }
30+ }
31+
32+ for (boolean b : marked ) {
33+ if (!b ) {
34+ return false ;
35+ }
36+ }
37+ return true ;
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ package strings ;
2+
3+ public class Upper {
4+
5+ /**
6+ * Driver Code
7+ */
8+ public static void main (String [] args ) {
9+ String [] strings = {"ABC" , "ABC123" , "abcABC" , "abc123ABC" };
10+ for (String s : strings ) {
11+ assert toUpperCase (s ).equals (s .toUpperCase ());
12+ }
13+ }
14+
15+ /**
16+ * Converts all of the characters in this {@code String} to upper case
17+ *
18+ * @param s the string to convert
19+ * @return the {@code String}, converted to uppercase.
20+ */
21+ public static String toUpperCase (String s ) {
22+ char [] values = s .toCharArray ();
23+ for (int i = 0 ; i < values .length ; ++i ) {
24+ if (Character .isLetter (values [i ]) && Character .isLowerCase (values [i ])) {
25+ values [i ] = Character .toUpperCase (values [i ]);
26+ }
27+ }
28+ return new String (values );
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments