0% found this document useful (0 votes)
5 views6 pages

String Array Questions

The document outlines several programming tasks in Java, including summing digits in a string, removing duplicate characters while preserving order, compressing strings, counting character frequencies, counting occurrences of integers in an array, and rearranging an array by sign. Each task includes input and output formats along with sample test cases to illustrate the expected functionality. The tasks focus on string manipulation, data structure usage, and algorithm implementation.

Uploaded by

Piyush Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

String Array Questions

The document outlines several programming tasks in Java, including summing digits in a string, removing duplicate characters while preserving order, compressing strings, counting character frequencies, counting occurrences of integers in an array, and rearranging an array by sign. Each task includes input and output formats along with sample test cases to illustrate the expected functionality. The tasks focus on string manipulation, data structure usage, and algorithm implementation.

Uploaded by

Piyush Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Write a program that takes a string containing both digits and the alphabet as input and finds the

sum of all
digits in the string.

Input format :
Input String containing both digits and alphabet.

Output format :
The Output Should display the Sum of all digits in the given string.

Sample test cases :

Input 1 :
Welc2ome87 to Java23

Output 1 :
The sum of digits in the string is: 22

Input 2 :
Pla53cement92season

Output 2 :
The sum of digits in the string is: 19
You are tasked with developing a utility for processing user- provided text inputs in an application. One of
the requirements is to ensure that any duplicate characters within a string input are removed to maintain
clarity and uniqueness in the output. However, the order of appearance of the first occurrence of each
character should be preserved to maintain the original context of the input.

Write a Java method that removes duplicate characters from a string while ensuring the order of the first
appearance of each character is retained.

Input Format
A single string input String containing the user-provided text.

Output Format
A single string with all duplicate characters removed, maintaining the order of the first appearance of
each character.

If there is no duplicate, then it should display "No duplicate characters found.“

Input ABCDAEBCDAB Output ABCDE


Input ABCD Output No duplicate characters found.
You are required to implement the following function:

public String compressString(String str);

The function accepts a string str as its argument. The function should perform basic string compression
using the counts of repeated characters. If the "compressed" string would not become smaller than the
original string, your method should return the original string.

Sample Testcase 1:

Explanation

The string str is "aabcccccaaa".


The compressed version of the string is "a2b1c5a3", which is shorter than the original string.

Sample Testcase 2:

Input ABCD
Output ABCD
Imagine you are developing a text processing application where you need to analyze the frequency of
each character in a user-provided text input.

Your application aims to provide statistical insights into the textual content, such as character
frequency distributions, without distinguishing between uppercase and lowercase letters using
HashMap

You need to implement a Java method that counts the occurrences of each character in a given string,
treating uppercase and lowercase versions of the same character as identical.

Input Format
It will take user input as string

Output Format
It should print each char and the count of the particular char

Input :
ABABCabCa

Output:
A4
B3
C2
You are required to implement the following function:

public static void countOccurrences (int[] array);

The function accepts an array of non-negative integers as its argument. The function should count the occurrences of
each number in the array and print the results.

Input Format
The first line should contain an integer representing the size of the array.
The second line should contain an array of integers.

Output Format
The output prints the result in the form of "number: count", where number is an element and count is number of
occurrences of an element.
Constraints The input array contains only non-negative integers.

Sample Input
9
123451234

Sample Output
1: 2
2: 2
3: 2
4: 2
5: 1
You are required to implement the following function:

public static int[] rearrangeBySign(int[] array);

The function accepts an array of integers as its argument. The function should rearrange the array so that
all positive numbers appear before all negative numbers. The relative order of the positive numbers and
negative numbers should be maintained.

Input Format
The first line should contain an integer representing the size of the array.
The second line should contain an array of integers.

Output Format
An array where all positive numbers appear before all negative numbers, maintaining the relative order.
Constraints
The input array contains both positive and negative integers.
Sample Input
6
-1 2 -3 4 -5 6
Sample Output
2 4 6 -1 -3 -5

You might also like