Programming Example StringDivision
Programming Example StringDivision
String Division
Write a program which reads a string from the user and divides the
string into different classes such as Strings, Numbers and
AlphanumericStrings etc. Create all possible classes.
getAllStrings function returns all the strings which are having only
characters.
getAllAlphanumericStrings function returns all the strings which are
having both combination of alphabets and numerals.
getAllNumerics function returns all the strings which are having only
numerals.
Example 1
Input
Output
Example 2
Input
Output
Package Name
File Name
Class Name
Function Name
:
:
:
:
test.stringdivision
StringDivision.java
StringDivision
public String[] getAllStrings(String str1)
public String[] getAllNumerics(String
str1)
public String[]
getAllAlphanumericStrings(String str1)
General Instructions
The package names class names, method signatures to be used
are mentioned in the problem statement.
Do not use your own names or change the method signatures and
fields. You can add any number of additional methods.
Pseudo Code
String division
1.
2.
3.
4.
5.
6.
Program Solution
package test.stringdivision;
public class StringDivision {
public String[] getAllStrings(String str1) {
char[] str = str1.toCharArray();
char[] ch = new char[20];
char[] arr = new char[50];
int k = 0, count = 0, i;
try {
for (i = 0; i < str.length;) {
while (str[i] == ' ') {
i++;
}
if (i == str.length) break;
for (; str[i] != ' '; i++) {
ch[k++] = str[i];
if (i + 1 == str.length) break;
}
char[] temp = new char[k];
int p = 0;
for (int x = 0; x < temp.length; x++)
temp[x] = ch[x];
if (checkString(temp, k) == 1) {
for (; p < temp.length; p++) {
arr[count++] = temp[p];
}
}
if (i + 1 != str.length && p > 0) {
arr[count] = ',';
count++;
}
if (i + 1 == str.length) break;
k = 0;
}
} catch (Throwable e) {
e.printStackTrace();
}
String fin = "";
String[] res = new String[50];
k = 0;
for (int j = 0; j < count; j++) {
if (arr[j] == ',' || j + 1 == count) {
if (arr[j] != ',') {
fin += arr[j];
}
res[k++] = fin;
fin = "";
} else fin += arr[j];
}
if (checkString(temp, k) == 3) {
for (p = 0; p < temp.length; p++) {
arr[count++] = temp[p];
}
}
if (i + 1 != str.length && p > 0) {
arr[count] = ',';
count++;
}
if (i + 1 == str.length) break;
k = 0;
}
} catch (Throwable e) {
e.printStackTrace();
}
String fin = "";
String[] res = new String[50];
k = 0;
for (int j = 0; j < count; j++) {
if (arr[j] == ',' || j + 1 == count) {
if (arr[j] != ',') {
fin += arr[j];
}
res[k++] = fin;
fin = "";
System.out.println(new
StringDivision().getAllStrings("the encrypted form of a key 12 is 2323jjj4334
code"));
System.out.println(new
StringDivision().getAllAlphanumericStrings("the encrypted form of a key 12 is
2323jjj4334 code"));
}
}