Posts

Showing posts with the label String Exercises

Check Whether One String Is Rotation Of Another | Java Programs

Output: *** Check Whether s1 is Rotated Version Of s2 *** s1 is: rogrammingExercisesJavaP s2 is: JavaProgrammingExercises s2 is a rotated version of s1 Click Here For  Java Online Compiler Solution: public class RotatedString { public static void main ( String [] args ) { System . out . println ( "*** Check Whether s1 is Rotated Version Of s2 ***" + "\n" ); String s1 = "rogrammingExercisesJavaP" ; String s2 = "JavaProgrammingExercises" ; System . out . println ( "s1 is: " + s1 ); System . out . println ( "s2 is: " + s2 + "\n" ); if ( s1 . length () != s2 . length ()) { System . out . println ( "s2 is not rotated version of s1" ); } else { String s3 = s1 + s1 ; if ( s3 . contains ( s2 )) { System . out . println ( "s2 is a rotated version o...

Reverse Each Word Of An String | Java Programs

Output: *** Reverse Each Word Of String *** Enter String: My name is alee Original String: My name is alee Reversing String Words: yM eman si eela Click Here For  Java Online Compiler Solution: import java.util.Scanner ; public class ReverseEachWord { static void reverseEachWord ( String string ) { String [] words = string . split ( " " ); String reverseString = "" ; int length = words . length ; for ( int i = 0; i < length ; i ++) { String word = words [ i ]; String reverseWord = "" ; for ( int j = word . length () - 1; j >= 0; j --) { reverseWord = reverseWord + word . charAt ( j ); } reverseString = reverseString + reverseWord + " " ; } System . out . println ( "\nOriginal String: " + string ); System . out ....

Convert All Characters In A String To Uppercase | Java Programs

Output: *** UpperCase All Characters In String *** Original String: This is a Test ! String in uppercase: THIS IS A TEST ! Click Here For  Java Online Compiler Solution: public class UppercaseString { public static void main ( String [] args ) { System . out . println ( "*** UpperCase All Characters In String ***" + "\n" ); String str = "This is a Test!" ; // Convert the above string to all uppercase. String upperStr = str . toUpperCase (); System . out . println ( "Original String: " + str ); System . out . println ( "String in uppercase: " + upperStr ); } }

Convert All Characters In A String To Lowercase | Java Programs

Output: *** LowerCase String *** Original String: This IS a TEst LowerCasingString: this is a test Click Here For  Java Online Compiler Solution: public class LowercaseString { public static void main ( String [] args ) { System . out . println ( "*** LowerCase String ***" + "\n" ); String str = "This IS a TEst" ; // Convert the above string to all lowercase. String lowerStr = str . toLowerCase (); System . out . println ( "Original String: " + str ); System . out . println ( "LowerCasingString: " + lowerStr ); } }

Trim Whitespaces From A String | Java Programs

Output: *** Trim Any Spaces From String *** Original String: Java Exercises New String: Java Exercises Click Here For  Java Online Compiler Solution: public class TrimString { public static void main ( String [] args ) { System . out . println ( "*** Trim Any Spaces From String ***" + "\n" ); String string = " Java Exercises" ; // Trim the whitespace from the front of the String. String newString = string . trim (); // Display the strings for comparison. System . out . println ( "Original String: " + string ); System . out . println ( "New String: " + newString ); } }

Get Substring Of A Given String | Java Programs

Output: *** Substring Of String *** String: The quick brown fox jumps over the lazy dog . Substring: quick Click Here For  Java Online Compiler Solution: public class Substring { public static void main ( String [] args ) { System . out . println ( "*** Substring Of String ***" + "\n" ); String string = "The quick brown fox jumps over the lazy dog." ; // Get a substring of the above string starting from // index 4 and ending at index 10. String substring = string . substring (4, 10); System . out . println ( "String: " + string ); System . out . println ( "Substring: " + substring ); } }

Replace One Character With The Other | Java Programs

Output: *** Replace Character With Other *** Original String: aleeee Replacing 'e' with 'y' New String: alyyyy Click Here For  Java Online Compiler Solution: class ReplaceChar { public static void main ( String [] args ) { System . out . println ( "*** Replace Character With Other ***" + "\n" ); String string = "aleeee" ; // Replace all the 'e' characters with 'y' characters. String newString = string . replace ( 'e' , 'y' ); // Display the strings for comparison. System . out . println ( "Original String: " + string + "\n" ); System . out . println ( "Replacing 'e' with 'y'" ); System . out . println ( "New String: " + newString ); } }

Find The Length Of An String | Java Programs

Output: *** Length of String *** The String is: Hi , My name is Alee ! Its length is: 20 Click Here For  Java Online Compiler Solution: class StringLength { public static void main ( String [] args ) { System . out . println ( "*** Length of String ***" + "\n" ); String s = "Hi, My name is Alee!" ; //length of the string int length = s . length (); System . out . println ( "The String is: " + s ); System . out . println ( "Its length is: " + length ); } }