PROJECT NO 2
STRING MANIPULATION
TOOL
*MOHAMMAD SHAOIB*
• Creating a class called
stringmanipulationtool
with main method
• Creating a scanner class
public class StringManipulationTool { called scanner.
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]); • Next taking a string
String input; variable as input.
• Taking do while loop for the
different maniputaing operations.
do {
[Link]("Java String Manipulation • Reason for do while because in
Tool");
[Link]("1. Reverse String"); this the code will execute first
[Link]("2. Count Characters");
[Link]("3. Count Words");
then it will check for the
[Link]("4. Uppercase"); condition
[Link]("5. Lowercase");
[Link]("6. Substring"); • Printing all the string
[Link]("7. Replace");
[Link]("8. Palindrome Check");
manipulations serially
[Link]("9. String Concatenation");
[Link]("0. Exit");
• To enter the choice of f operation
a scanner is created called choice
[Link]("Enter your choice: ");
int choice = [Link]();
• SWITCH CASE
• Here we are taking switch to take all
tbe string manipulation one by one
switch (choice) { in the form of cases
case 1:
[Link]("Enter a string: "); • In case 1 we have taken a scanner
input = [Link]();
[Link]("Reversed String: " +
called input and printing reverse of
reverseString(input)); string.
break;
case 2: • For counting characters scanner is
[Link]("Enter a string: ");
input = [Link]();
used and printing the
[Link]("Number of characters: " + countcharacters
countCharacters(input));
break; • Similarly for all the other string
manipulations we repeat the same
procedure.
• In the final case we are exiting the
code .
• If the user input is not in the range
of switch cases then it will go to
default .
case 0:
[Link]("Exiting the program. • While loop is used to create a
Goodbye!");
break; infinte loop of the statements untill
default:
[Link]("Invalid choice. Please enter
the condition is true the loop is
a valid option."); executed .
}
} while (true);
}
CREATING METHODS :
• REVERSE: A string builder is created
and reverse operation is performed
on the string.
public static String reverseString(String input) {
return new StringBuilder(input).reverse().toString(); • COUNTCHARACTERS: The length of
}
the string is obtained by taking
public static int countCharacters(String input) {
return [Link]();
[Link]().
} • COUNT WORDS:The string is split by
public static int countWords(String input) { using [Link]().and then taking
String[] words = [Link]("\\s+");
return [Link]; [Link]
}
• SUBSTRING:In substring the strat
public static String getSubstring(String input, int
startIndex, int endIndex) { index and the end index is taken to
return [Link](startIndex, endIndex); cut yhe string . Syntax is
}
[Link](start end);
public static String replaceSubstring(String input,
String toReplace, String replacement) { • REPLACE: A string can be replaced by
}
return [Link](toReplace, replacement); other string by using [Link](to
replace,replacement);
public static boolean isPalindrome(String input) {
String reversed = reverseString(input); • IS PALINDROME: A word when read
}
return [Link](reversed);
normal and reversed should be same
public static String concatenateStrings(String first,
• CONCATINATION :Adding of two or
String second) { more strings together in the project
return first + second;
} we added the first and the second
} string.
THANKYOU