Answers for Strings
Q1: Reverse a String
java
Copy code
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String input = sc.nextLine();
String reversed = "";
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input.charAt(i);
}
System.out.println("Reversed string: " + reversed);
}
}
Q2: Arrange Characters in a String
java
Copy code
import java.util.Arrays;
public class ArrangeString {
public static void main(String[] args) {
String input = "ram#123";
StringBuilder numbers = new StringBuilder();
StringBuilder symbols = new StringBuilder();
StringBuilder letters = new StringBuilder();
for (char ch : input.toCharArray()) {
if (Character.isDigit(ch)) {
numbers.append(ch);
} else if (Character.isLetter(ch)) {
letters.append(ch);
} else {
symbols.append(ch);
}
}
System.out.println(numbers.toString() + symbols.toString() + letters.toString());
}
}
Q3: Count Words in a String
java
Copy code
import java.util.Scanner;
public class WordCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String input = sc.nextLine();
String[] words = input.split("\\s+");
System.out.println("Number of words: " + words.length);
}
}
Q4: Check Palindrome
java
Copy code
import java.util.Scanner;
public class PalindromeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String input = sc.nextLine();
String reversed = new StringBuilder(input).reverse().toString();
if (input.equals(reversed)) {
System.out.println("The string is a palindrome.");
} else {
System.out.println("The string is not a palindrome.");
}
}
}
Q5: Convert String to Char Array Without Using Built-in Function
java
Copy code
import java.util.Scanner;
public class StringToCharArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String input = sc.nextLine();
char[] charArray = new char[input.length()];
for (int i = 0; i < input.length(); i++) {
charArray[i] = input.charAt(i);
}
System.out.print("Character Array: ");
for (char c : charArray) {
System.out.print(c + " ");
}
}
}
Q6: Sum of Numbers in a String
java
Copy code
import java.util.Scanner;
public class SumOfNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String input = sc.nextLine();
int sum = 0;
for (char ch : input.toCharArray()) {
if (Character.isDigit(ch)) {
sum += Character.getNumericValue(ch);
}
}
System.out.println("Sum of numbers: " + sum);
}
}
Q7: Find Index of a Character
java
Copy code
import java.util.Scanner;
public class FindIndex {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String input = sc.nextLine();
System.out.println("Enter the character to find:");
char key = sc.next().charAt(0);
int index = input.indexOf(key);
if (index != -1) {
System.out.println("Index of '" + key + "': " + index);
} else {
System.out.println("Character not found.");
}
}
}
Would you like additional solutions or modifications for these? Let me know!
4o
You said:
i want all 50 questions answer
ChatGPT said:
ChatGPT
Certainly! I'll provide answers to all 50 questions, but due to the constraints of this
environment, I’ll share them incrementally. Here's how we'll approach it:
I'll provide solutions for the first 10 questions in detail.
Then continue to subsequent sets of 10 questions until all 50 are covered.
Let’s get started with Q1 to Q10.
Q1: Reverse a String
java
Copy code
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String input = sc.nextLine();
String reversed = "";
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input.charAt(i);
}
System.out.println("Reversed string: " + reversed);
}
}
Q2: Arrange Characters in a String
java
Copy code
import java.util.Scanner;
public class ArrangeString {
public static void main(String[] args) {
String input = "ram#123";
StringBuilder digits = new StringBuilder();
StringBuilder symbols = new StringBuilder();
StringBuilder letters = new StringBuilder();
for (char ch : input.toCharArray()) {
if (Character.isDigit(ch)) {
digits.append(ch);
} else if (Character.isLetter(ch)) {
letters.append(ch);
} else {
symbols.append(ch);
}
}
System.out.println(digits.toString() + symbols.toString() + letters.toString());
}
}
Q3: Count Words in a String
java
Copy code
import java.util.Scanner;
public class CountWords {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String input = sc.nextLine();
String[] words = input.trim().split("\\s+");
System.out.println("Number of words: " + words.length);
}
}
Q4: Check Palindrome
java
Copy code
import java.util.Scanner;
public class PalindromeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String input = sc.nextLine();
String reversed = new StringBuilder(input).reverse().toString();
if (input.equals(reversed)) {
System.out.println("The string is a palindrome.");
} else {
System.out.println("The string is not a palindrome.");
}
}
}
Q5: Convert String to Char Array Without Using Built-in Function
java
Copy code
import java.util.Scanner;
public class StringToCharArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String input = sc.nextLine();
char[] charArray = new char[input.length()];
for (int i = 0; i < input.length(); i++) {
charArray[i] = input.charAt(i);
}
System.out.print("Character Array: ");
for (char c : charArray) {
System.out.print(c + " ");
}
}
}
Q6: Sum of Numbers in a String
java
Copy code
import java.util.Scanner;
public class SumOfNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String input = sc.nextLine();
int sum = 0;
for (char ch : input.toCharArray()) {
if (Character.isDigit(ch)) {
sum += Character.getNumericValue(ch);
}
}
System.out.println("Sum of numbers: " + sum);
}
}
Q7: Find Index of a Character
java
Copy code
import java.util.Scanner;
public class FindCharacterIndex {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String input = sc.nextLine();
System.out.println("Enter a character to find:");
char key = sc.next().charAt(0);
int index = input.indexOf(key);
if (index != -1) {
System.out.println("Index of '" + key + "': " + index);
} else {
System.out.println("Character not found.");
}
}
}
Q8: Count Each Character's Occurrence
java
Copy code
import java.util.HashMap;
public class CharacterCount {
public static void main(String[] args) {
String input = "AssmnAN";
HashMap<Character, Integer> charCountMap = new HashMap<>();
for (char ch : input.toCharArray()) {
charCountMap.put(ch, charCountMap.getOrDefault(ch, 0) + 1);
}
for (char ch : charCountMap.keySet()) {
System.out.println(ch + "=" + charCountMap.get(ch));
}
}
}
Q9: Replace Characters with @
java
Copy code
public class ReplaceCharacters {
public static void main(String[] args) {
String input = "Ramana";
String output = input.replaceAll("[aeiouAEIOU]", "@");
System.out.println(output);
}
}
Q10: Count Vowel Characters
java
Copy code
public class CountVowels {
public static void main(String[] args) {
String input = "Ramana";
int vowelCount = 0;
for (char ch : input.toCharArray()) {
if ("aeiouAEIOU".indexOf(ch) != -1) {
vowelCount++;
}
}
System.out.println("Number of vowels: " + vowelCount);
}
}
Let me know when you're ready for Q11 to Q20!
4o
You said:
i am ready
ChatGPT said:
ChatGPT
Great! Let's continue with Q11 to Q20.
Q11: Print Vowel Characters in a String
java
Copy code
public class PrintVowels {
public static void main(String[] args) {
String input = "Ramana";
System.out.print("Vowels in the string: ");
for (char ch : input.toCharArray()) {
if ("aeiouAEIOU".indexOf(ch) != -1) {
System.out.print(ch + " ");
}
}
}
}
Q12: Maximum Occurrence Character
java
Copy code
import java.util.HashMap;
public class MaxOccurrenceCharacter {
public static void main(String[] args) {
String input = "ramana";
HashMap<Character, Integer> charCount = new HashMap<>();
for (char ch : input.toCharArray()) {
charCount.put(ch, charCount.getOrDefault(ch, 0) + 1);
}
char maxChar = ' ';
int maxCount = 0;
for (char ch : charCount.keySet()) {
if (charCount.get(ch) > maxCount) {
maxCount = charCount.get(ch);
maxChar = ch;
}
}
System.out.println(maxChar + "=" + maxCount);
}
}
Q13: Minimum Occurrence Character
java
Copy code
import java.util.HashMap;
public class MinOccurrenceCharacter {
public static void main(String[] args) {
String input = "ramana";
HashMap<Character, Integer> charCount = new HashMap<>();
for (char ch : input.toCharArray()) {
charCount.put(ch, charCount.getOrDefault(ch, 0) + 1);
}
char minChar = ' ';
int minCount = Integer.MAX_VALUE;
for (char ch : charCount.keySet()) {
if (charCount.get(ch) < minCount) {
minCount = charCount.get(ch);
minChar = ch;
}
}
System.out.println(minChar + "=" + minCount);
}
}
Q14: Transform to Alternating Digits
java
Copy code
public class TransformToDigits {
public static void main(String[] args) {
String input = "helloworld";
StringBuilder output = new StringBuilder();
int digit = 1;
for (int i = 0; i < input.length(); i++) {
if (i % 2 == 0) {
output.append(input.charAt(i));
} else {
output.append(digit++);
}
}
System.out.println(output.toString());
}
}
Q15: Toggle Case Alternating
java
Copy code
public class ToggleCase {
public static void main(String[] args) {
String input = "RaMana";
StringBuilder output = new StringBuilder();
for (char ch : input.toCharArray()) {
if (Character.isUpperCase(ch)) {
output.append(Character.toLowerCase(ch));
} else {
output.append(Character.toUpperCase(ch));
}
}
System.out.println(output.toString());
}
}
Q16: Convert to InitCap for Each Word
java
Copy code
public class InitCap {
public static void main(String[] args) {
String input = "india that is bharat";
String[] words = input.split(" ");
StringBuilder output = new StringBuilder();
for (String word : words) {
output.append(Character.toUpperCase(word.charAt(0)))
.append(word.substring(1).toLowerCase())
.append(" ");
}
System.out.println(output.toString().trim());
}
}
Q17: Reverse Words in a Sentence
java
Copy code
public class ReverseWords {
public static void main(String[] args) {
String input = "java is very easy";
String[] words = input.split(" ");
StringBuilder output = new StringBuilder();
for (String word : words) {
output.append(new StringBuilder(word).reverse()).append(" ");
}
System.out.println(output.toString().trim());
}
}
Q18: Print Palindrome Words in a String
java
Copy code
public class PalindromeWords {
public static void main(String[] args) {
String input = "eye level car boy";
String[] words = input.split(" ");
for (String word : words) {
if (word.equals(new StringBuilder(word).reverse().toString())) {
System.out.println(word);
}
}
}
}
Q19: Change Uppercase to Lowercase
java
Copy code
public class ChangeCase {
public static void main(String[] args) {
String input = "RaMana";
String output = input.toLowerCase();
System.out.println(output);
}
}
Q20: Print Incremental Substrings
java
Copy code
public class IncrementalSubstring {
public static void main(String[] args) {
String input = "Ramana";
for (int i = 1; i <= input.length(); i++) {
System.out.println(input.substring(0, i));
}
}
}
Would you like me to proceed with Q21 to Q30? Let me know!
4o
You said:
yes
ChatGPT said:
ChatGPT
Here are solutions for Q21 to Q30:
Q21: Reverse the Order of Words in a String
java
Copy code
public class ReverseSentence {
public static void main(String[] args) {
String input = "easy very is java";
String[] words = input.split(" ");
StringBuilder output = new StringBuilder();
for (int i = words.length - 1; i >= 0; i--) {
output.append(words[i]).append(" ");
}
System.out.println(output.toString().trim());
}
}
Q22: Print Words in a String Line by Line
java
Copy code
public class WordsInNewLine {
public static void main(String[] args) {
String input = "Java Is Very Easy";
String[] words = input.split(" ");
for (String word : words) {
System.out.println(word);
}
}
}
Q23: Sort a String
java
Copy code
import java.util.Arrays;
public class SortString {
public static void main(String[] args) {
String input = "ccaabbd";
char[] chars = input.toCharArray();
Arrays.sort(chars);
System.out.println(new String(chars));
}
}
Q24: Print All Substrings of a String
java
Copy code
public class Substrings {
public static void main(String[] args) {
String input = "Malayalam";
for (int i = 0; i < input.length(); i++) {
for (int j = i + 1; j <= input.length(); j++) {
System.out.println(input.substring(i, j));
}
}
}
}
Q25: Remove Vowels in String
java
Copy code
public class RemoveVowels {
public static void main(String[] args) {
String input = "ramana";
StringBuilder output = new StringBuilder();
for (char ch : input.toCharArray()) {
if ("aeiouAEIOU".indexOf(ch) == -1) {
output.append(ch);
}
}
System.out.println(output.toString());
}
}
Q26: Find Missing Alphabets in a String
java
Copy code
public class MissingAlphabets {
public static void main(String[] args) {
String input = "abc";
StringBuilder missing = new StringBuilder();
for (char ch = 'a'; ch <= 'z'; ch++) {
if (input.indexOf(ch) == -1) {
missing.append(ch).append(" ");
}
}
System.out.println(missing.toString().trim());
}
}
Q27: Print Integer Without parseInt
java
Copy code
public class StringToInteger {
public static void main(String[] args) {
String input = "3124";
int number = 0;
for (char ch : input.toCharArray()) {
number = number * 10 + (ch - '0');
}
System.out.println("Converted number: " + number);
}
}
Q28: Replace Substring
java
Copy code
public class ReplaceSubstring {
public static void main(String[] args) {
String input = "I love python";
String output = input.replace("python", "java");
System.out.println(output);
}
}
Q29: Check if Strings are Panagrams
java
Copy code
public class PanagramCheck {
public static void main(String[] args) {
String str1 = "The quick brown fox jumps over the lazy dog";
boolean[] alphabet = new boolean[26];
str1 = str1.toLowerCase().replaceAll("[^a-z]", "");
for (char ch : str1.toCharArray()) {
alphabet[ch - 'a'] = true;
}
boolean isPanagram = true;
for (boolean exists : alphabet) {
if (!exists) {
isPanagram = false;
break;
}
}
System.out.println("Is panagram: " + isPanagram);
}
}
Q30: Check if Strings are Anagrams
java
Copy code
import java.util.Arrays;
public class AnagramCheck {
public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
boolean isAnagram = Arrays.equals(arr1, arr2);
System.out.println("Are anagrams: " + isAnagram);
}
}
Let me know when you're ready for Q31 to Q40!
4o
You said:
yes
ChatGPT said:
ChatGPT
Here are the solutions for Q31 to Q40.
Q31: Find Missing Vowels in a String
java
Copy code
public class MissingVowels {
public static void main(String[] args) {
String input = "xyz";
String vowels = "aeiou";
StringBuilder missing = new StringBuilder();
for (char ch : vowels.toCharArray()) {
if (input.indexOf(ch) == -1) {
missing.append(ch).append(" ");
}
}
System.out.println("Missing vowels: " + missing.toString().trim());
}
}
Q32: Occurrence of a Character Using Replace
java
Copy code
public class OccurrenceUsingReplace {
public static void main(String[] args) {
String input = "ash123";
char ch = 's';
int count = input.length() - input.replace(String.valueOf(ch), "").length();
System.out.println("Occurrence of '" + ch + "': " + count);
}
}
Q33: Split String into Array Without Using split()
java
Copy code
import java.util.ArrayList;
public class SplitString {
public static void main(String[] args) {
String input = "java is easy";
ArrayList<String> words = new ArrayList<>();
StringBuilder word = new StringBuilder();
for (char ch : input.toCharArray()) {
if (ch == ' ') {
words.add(word.toString());
word = new StringBuilder();
} else {
word.append(ch);
}
}
words.add(word.toString()); // Add the last word.
System.out.println(words);
}
}
Q34: Reverse Order of Elements in a String Array
java
Copy code
public class ReverseArray {
public static void main(String[] args) {
String[] arr = {"apple", "banana", "cherry", "date", "elderberry"};
System.out.println("Reversed array:");
for (int i = arr.length - 1; i >= 0; i--) {
System.out.println(arr[i]);
}
}
}
Q35: All Permutations of a String
java
Copy code
public class Permutations {
public static void permute(String str, String result) {
if (str.isEmpty()) {
System.out.println(result);
return;
}
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
String rest = str.substring(0, i) + str.substring(i + 1);
permute(rest, result + ch);
}
}
public static void main(String[] args) {
String input = "abc";
permute(input, "");
}
}
Q36: Find Palindromic Substrings
java
Copy code
public class PalindromicSubstrings {
public static void main(String[] args) {
String input = "Malayalam";
for (int i = 0; i < input.length(); i++) {
for (int j = i + 1; j <= input.length(); j++) {
String substring = input.substring(i, j);
if (substring.equals(new StringBuilder(substring).reverse().toString())) {
System.out.println(substring);
}
}
}
}
}
Q37: Remove Duplicate Words
java
Copy code
import java.util.LinkedHashSet;
public class RemoveDuplicateWords {
public static void main(String[] args) {
String input = "happy happy birthday";
String[] words = input.split(" ");
LinkedHashSet<String> uniqueWords = new LinkedHashSet<>();
for (String word : words) {
uniqueWords.add(word);
}
System.out.println(String.join(" ", uniqueWords));
}
}
Q38: Smallest Palindromic Substring
java
Copy code
public class SmallestPalindrome {
public static void main(String[] args) {
String input = "Malayalam";
String smallest = null;
for (int i = 0; i < input.length(); i++) {
for (int j = i + 1; j <= input.length(); j++) {
String substring = input.substring(i, j);
if (substring.equals(new StringBuilder(substring).reverse().toString())) {
if (smallest == null || substring.length() < smallest.length()) {
smallest = substring;
}
}
}
}
System.out.println("Smallest palindrome: " + smallest);
}
}
Q39: Swap Two Strings Without Third Variable
java
Copy code
public class SwapStrings {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "world";
str1 = str1 + str2;
str2 = str1.substring(0, str1.length() - str2.length());
str1 = str1.substring(str2.length());
System.out.println("After swapping: str1 = " + str1 + ", str2 = " + str2);
}
}
Q40: Sum Numbers in Mixed String
java
Copy code
public class SumMixedString {
public static void main(String[] args) {
String input = "acb243d10";
StringBuilder num = new StringBuilder();
int total = 0;
for (char ch : input.toCharArray()) {
if (Character.isDigit(ch)) {
num.append(ch);
} else if (num.length() > 0) {
total += Integer.parseInt(num.toString());
num = new StringBuilder();
}
}
if (num.length() > 0) {
total += Integer.parseInt(num.toString());
}
System.out.println("Sum: " + total);
}
}
Let me know when you're ready for Q41 to Q50!
4o
You said:
yes
ChatGPT said:
ChatGPT
Here are the solutions for Q41 to Q50.
Q41: Sum of ASCII Values of Characters
java
Copy code
public class ASCIISum {
public static void main(String[] args) {
String input = "abc";
int sum = 0;
for (char ch : input.toCharArray()) {
sum += (int) ch; // Add ASCII value of each character.
}
System.out.println("Sum of ASCII values: " + sum);
}
}
Q42: Remove Spaces Without Using Inbuilt Function
java
Copy code
public class RemoveSpaces {
public static void main(String[] args) {
String input = "java is easy";
StringBuilder output = new StringBuilder();
for (char ch : input.toCharArray()) {
if (ch != ' ') {
output.append(ch);
}
}
System.out.println("String without spaces: " + output.toString());
}
}
Q43: Count Characters in Each Word
java
Copy code
public class CountCharacters {
public static void main(String[] args) {
String input = "java is easy";
String[] words = input.split(" ");
for (String word : words) {
System.out.println(word + "=" + word.length());
}
}
}
Q44: Occurrence of a Character Using replace()
java
Copy code
public class CharacterOccurrence {
public static void main(String[] args) {
String input = "programming";
char key = 'g';
int count = input.length() - input.replace(String.valueOf(key), "").length();
System.out.println("Occurrence of '" + key + "': " + count);
}
}
Q45: Remove Extra Spaces
java
Copy code
public class RemoveExtraSpaces {
public static void main(String[] args) {
String input = "__java__is___very__easy__";
String output = input.replaceAll("_+", "_"); // Replace multiple underscores with
a single one.
System.out.println("After removing extra spaces: " + output);
}
}
Q46: Check Anagram Without replace()
java
Copy code
import java.util.Arrays;
public class AnagramWithoutReplace {
public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
boolean isAnagram = Arrays.equals(arr1, arr2);
System.out.println("Are anagrams: " + isAnagram);
}
}
Q47: Remove Vowels Using indexOf()
java
Copy code
public class RemoveVowelsUsingIndexOf {
public static void main(String[] args) {
String input = "ramana";
String vowels = "aeiouAEIOU";
StringBuilder output = new StringBuilder();
for (char ch : input.toCharArray()) {
if (vowels.indexOf(ch) == -1) {
output.append(ch);
}
}
System.out.println("String after removing vowels: " + output.toString());
}
}
Q48: Find Missing Alphabets Using indexOf()
java
Copy code
public class MissingAlphabetsUsingIndexOf {
public static void main(String[] args) {
String input = "abc";
StringBuilder missing = new StringBuilder();
for (char ch = 'a'; ch <= 'z'; ch++) {
if (input.indexOf(ch) == -1) {
missing.append(ch).append(" ");
}
}
System.out.println("Missing alphabets: " + missing.toString().trim());
}
}
Q49: Remove Edge Spaces Using trim()
java
Copy code
public class RemoveEdgeSpaces {
public static void main(String[] args) {
String input = "__java _is _easy__";
String output = input.trim().replaceAll("_+", " ").trim();
System.out.println("String after removing edge spaces: " + output);
}
}
Q50: Replace Vowels If Count > 3
java
Copy code
public class ReplaceVowelsIfMoreThanThree {
public static void main(String[] args) {
String input = "abeijho";
int vowelCount = 0;
for (char ch : input.toCharArray()) {
if ("aeiouAEIOU".indexOf(ch) != -1) {
vowelCount++;
}
}
if (vowelCount > 3) {
String output = input.replaceAll("[aeiouAEIOU]", "_");
System.out.println("Modified String: " + output);
} else {
System.out.println("String remains unchanged: " + input);
}
}
}