Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mananguptamg/java-string
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: mananguptamg/java-string
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: level-extra-problems
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 12 commits
  • 12 files changed
  • 1 contributor

Commits on Mar 6, 2025

  1. Copy the full SHA
    f4915d6 View commit details
  2. Copy the full SHA
    dc23b75 View commit details
  3. Copy the full SHA
    5f03425 View commit details

Commits on Mar 7, 2025

  1. Copy the full SHA
    054195b View commit details
  2. Copy the full SHA
    183acf4 View commit details
  3. Copy the full SHA
    6ae18bb View commit details
  4. Copy the full SHA
    b4ac617 View commit details
  5. Copy the full SHA
    45d0cc5 View commit details
  6. Copy the full SHA
    03ee027 View commit details
  7. Copy the full SHA
    3688cc0 View commit details
  8. Copy the full SHA
    2433b9e View commit details
  9. Copy the full SHA
    b9824fc View commit details
Showing with 489 additions and 0 deletions.
  1. +51 −0 AnagramStrings.java
  2. +41 −0 LexicographicString.java
  3. +28 −0 LongestWord.java
  4. +42 −0 MostFrequentCharacter.java
  5. +75 −0 PalindromeString.java
  6. +32 −0 RemoveCharacter.java
  7. +26 −0 RemoveDuplicates.java
  8. +38 −0 ReplaceWord.java
  9. +26 −0 ReverseString.java
  10. +30 −0 SubstringOccurences.java
  11. +32 −0 ToggleCase.java
  12. +68 −0 VowelsAndConsonants.java
51 changes: 51 additions & 0 deletions AnagramStrings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import java.util.Scanner;

class AnagramStrings {
// method to check if two strings are anagrams
public static boolean areAnagrams(String s1, String s2) {
// Check if lengths are equal
if (s1.length() != s2.length()) {
return false;
}

// Create frequency arrays for both texts (ASCII size: 256)
int[] frequency1 = new int[256];
int[] frequency2 = new int[256];

// Count frequency of each character in both texts
for (int i = 0; i < s1.length(); i++) {
frequency1[s1.charAt(i)]++;
frequency2[s2.charAt(i)]++;
}

// Compare the frequency arrays
for (int i = 0; i < 256; i++) {
if (frequency1[i] != frequency2[i]) {
return false;
}
}
return true;
}

// Main method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter first text: ");
String text1 = input.nextLine();

System.out.print("Enter second text: ");
String text2 = input.nextLine();

// Convert both strings to lowercase and remove spaces
text1 = text1.toLowerCase().replaceAll("\\s", "");
text2 = text2.toLowerCase().replaceAll("\\s", "");

// Check if the texts are anagrams
if (areAnagrams(text1, text2)) {
System.out.println("The texts are anagrams");
} else {
System.out.println("The texts are not anagrams");
}
}
}
41 changes: 41 additions & 0 deletions LexicographicString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.Scanner;

class LexicographicString {
// Main method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter first string: ");
String str1 = input.nextLine();

System.out.print("Enter second string: ");
String str2 = input.nextLine();

// Method to compare two strings lexicographically
int result = compareStrings(str1, str2);

// Display result
if (result < 0) {
System.out.printf("\"%s\" comes before \"%s\" in lexicographical order", str1, str2);
} else if (result > 0) {
System.out.printf("\"%s\" comes before \"%s\" in lexicographical order", str2, str1);
} else {
System.out.printf("\"%s\" and \"%s\" are equal in lexicographical order", str1, str2);
}
}

// Method to compare two strings lexicographically
public static int compareStrings(String str1, String str2) {
int len1 = str1.length();
int len2 = str2.length();
int minLen = Math.min(len1, len2);

for (int i = 0; i < minLen; i++) {
if (str1.charAt(i) != str2.charAt(i)) {
return str1.charAt(i) - str2.charAt(i);
}
}

return len1 - len2;
}
}
28 changes: 28 additions & 0 deletions LongestWord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Scanner;

class LongestWord {
// Main method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter a string: ");
String str = input.nextLine();

// Method to find the longest word in a string
String result = longestWord(str);

System.out.println("Longest word in the string: " + result);
}

// Method to find the longest word in a string
public static String longestWord(String str) {
String[] words = str.split(" ");
String longestWord = "";
for (String word : words) {
if (word.length() > longestWord.length()) {
longestWord = word;
}
}
return longestWord;
}
}
42 changes: 42 additions & 0 deletions MostFrequentCharacter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.Scanner;

class MostFrequentCharacter {
// Main method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter a string: ");
String str = input.nextLine();

// Method to find the most frequent character in a string
char result = mostFrequentChar(str);

System.out.println("Most frequent character in the string: " + result);
}

// Method to find the most frequent character in a string
public static char mostFrequentChar(String str) {
// Remove all leading and trailing whitespace, and in between spaces
str = str.replaceAll("\\s", "").trim();

int[] freq = new int[256];

// Count frequency of each character
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
freq[ch]++;
}

// Find the character with maximum frequency
char mostFrequentChar = '\0';
int maxFreq = 0;
for (int i = 0; i < freq.length; i++) {
if (freq[i] > maxFreq) {
maxFreq = freq[i];
mostFrequentChar = (char) i;
}
}

return mostFrequentChar;
}
}
75 changes: 75 additions & 0 deletions PalindromeString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import java.util.Scanner;

class PalindromeString {
// Logic 1: using two-pointers approach
public static boolean isPalindromeStringTwoPointer(String s) {
int start = 0, end = s.length() - 1;
while (start < end) {
if (s.charAt(start) != s.charAt(end)) {
return false;
}
start++;
end--;
}
return true;
}

// Logic 2: Using Recursion approach
public static boolean isPalindromeStringRecursive(String s, int start, int end) {
if (start >= end) {
return true;
}
if (s.charAt(start) != s.charAt(end)) {
return false;
}
return isPalindromeStringRecursive(s, start + 1, end - 1);
}

// Logic 3: Using character array and reverse comparison approach
public static boolean isPalindromeStringCharArray(String s) {
char[] originalArray = s.toCharArray();
char[] reversedArray = reverseString(s);

// Compare original and reversed arrays
for (int i = 0; i < originalArray.length; i++) {
if (originalArray[i] != reversedArray[i]) {
return false;
}
}
return true;
}

// Method to reverse a string
public static char[] reverseString(String s) {
char[] reversed = new char[s.length()];
for (int i = 0; i < s.length(); i++) {
reversed[i] = s.charAt(s.length() - 1 - i);
}
return reversed;
}

// Main method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter a string: ");
String userInput = input.nextLine();

// Checking using all three logic methods
boolean result1 = isPalindromeStringTwoPointer(userInput);
boolean result2 = isPalindromeStringRecursive(userInput, 0, userInput.length() - 1);
boolean result3 = isPalindromeStringCharArray(userInput);

// Display results
System.out.println("[Logic 1] Is the string a palindrome? " + result1);
System.out.println("[Logic 2] Is the string a palindrome? " + result2);
System.out.println("[Logic 3] Is the string a palindrome? " + result3);

// Compare the results
if (result1 == result2 && result2 == result3) {
System.out.println("All methods give the same result.");
} else {
System.out.println("Results are not the same.");
}
}
}
32 changes: 32 additions & 0 deletions RemoveCharacter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.Scanner;

class RemoveCharacter {
// Main method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter a string: ");
String str = input.nextLine();

System.out.print("Enter a character to remove: ");
char ch = input.next().charAt(0);

// Method to remove a specific character from the string
String result = removeCharacter(str, ch);

System.out.println("String after removing character: " + result);

input.close();
}

// Method to remove a specific character from the string
public static String removeCharacter(String str, char ch) {
String result = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ch) {
result += str.charAt(i);
}
}
return result;
}
}
26 changes: 26 additions & 0 deletions RemoveDuplicates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.Scanner;

class RemoveDuplicates {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter a string: ");
String str = input.nextLine();

// Method to remove duplicates from a string
String result = removeDuplicates(str);

System.out.println("String after removing duplicates: " + result);
}

// Method to remove duplicates from a string
public static String removeDuplicates(String str) {
String result = "";
for (int i = 0; i < str.length(); i++) {
if (result.indexOf(str.charAt(i)) == -1) {
result += str.charAt(i);
}
}
return result;
}
}
38 changes: 38 additions & 0 deletions ReplaceWord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.Scanner;

class ReplaceWord {
// Main Method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter a string: ");
String str = input.nextLine();

System.out.print("Enter a word to replace: ");
String wordToReplace = input.next();

System.out.print("Enter a word to replace with: ");
String wordToReplaceWith = input.next();

// Method to replace a given word in string with another word
String result = replaceWord(str, wordToReplace, wordToReplaceWith);

System.out.println("String after replacing word: " + result);

input.close();
}

// Method to replace a given word in string with another word
public static String replaceWord(String str, String wordToReplace, String wordToReplaceWith) {
String[] words = str.split(" ");
String result = "";
for (String word : words) {
if (word.equals(wordToReplace)) {
result += wordToReplaceWith + " ";
} else {
result += word + " ";
}
}
return result;
}
}
26 changes: 26 additions & 0 deletions ReverseString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.Scanner;

public class ReverseString {
// Method to reverse a string without built-in functions
public static String reverse(String input) {
String reversed = "";
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input.charAt(i); // Append characters in reverse order
}
return reversed;
}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

// Taking user input
System.out.print("Enter a string: ");
String userInput = input.nextLine();

// Calling the reverse method
String reversedString = reverse(userInput);

// Displaying the result
System.out.println("Reversed String: " + reversedString);
}
}
30 changes: 30 additions & 0 deletions SubstringOccurences.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.Scanner;

class SubstringOccurences {
// Main method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter a string: ");
String str = input.nextLine();

System.out.print("Enter a substring: ");
String subStr = input.nextLine();

// Method to calculate number of times a substring occurs in the String
int result = substringOccurences(str, subStr);

System.out.println("Number of times the substring " +subStr+ " occurs in the string: " + result);
}

// Method to calculate number of times a substring occurs in the String
public static int substringOccurences(String str, String subStr) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.substring(i).startsWith(subStr)) {
count++;
}
}
return count;
}
}
Loading