You are on page 1of 14

Programming Examples

Name of the Program: String Division


Level : Difficult

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.

The Prototype of the Function is :


public String[] getAllStrings(String str1)
The method getAllStrings takes the string str1 as input and returns all
the strings which are having only characters as a string array.
public String[] getAllNumerics(String str1)
The method getAllNumerics takes the string str1 as input and returns
all the strings which are having both combination of alphabets and
numerals as a string array.
public String[] getAllAlphanumericStrings(String str1)
The method getAllNumerics takes the string str1 as input and returns
all the strings which are having only numerical as a string array.
Constraints
Consider that input is a combination of alphabets and numerals
only.

Example 1
Input
Output

Example 2
Input
Output

String str1 = "h3llo this 1s 4 input string 001"


getAllStrings Returns : {"this", "input", "string"};
getAllAlphanumericStrings Returns : {"h3llo", "1s"};
getAllNumerics Returns : {"4", "001"};

String str1 ="6575 DevSquare is a1n online


development 2007 environment65"
getAllStrings Returns : {"DevSquare","is", "online",
"development"};
getAllNumerics Returns : {"6575", "2007"};
getAllAlphanumericStrings Returns : {"a1n",
"environment65"};

For Java solutions

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.

Pass the input string to the method getAllStrings().


Return all the strings which are having only characters from the
method getAllStrings().
Pass the input string to the method getAllAlphanumericStrings().
Return all the strings which are having both combination of
alphabets and numerals from the method
getAllAlphanumericStrings().
Pass the input string to the method getAllNumerics().
Return all the strings which are having only numerals from the
method getAllNumerics().

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];
}

String[] res1 = new String[k];


System.arraycopy(res, 0, res1, 0, k);
return res1;
}
public String[] getAllNumerics(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) == 2) {

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];


}
String[] res1 = new String[k];
//Array.Copy(res,0,res1,0,k);
System.arraycopy(res, 0, res1, 0, k);
return res1;
}
public String[] getAllAlphanumericStrings(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) == 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 = "";

} else fin += arr[j];


}
String[] res1 = new String[k];
//Array.Copy(res,0,res1,0,k);
System.arraycopy(res, 0, res1, 0, k);
return res1;
}
int checkString(char[] str, int k) {
int flag1 = 0, flag2 = 0;
for (int i = 0; i < str.length; i++) {
if (str[i] >= 48 && str[i] <= 57) flag1 = 1;
else if ((str[i] >= 65 && str[i] <= 90) || (str[i] >= 97 && str[i] <= 122)) flag2
= 1;
}
if (flag2 == 1 && flag1 == 0) return 1;
if (flag2 == 0 && flag1 == 1) return 2;
if (flag2 == 1 && flag1 == 1) return 3;
return 1;
}
public static void main(String[] args)
{
System.out.println(new
StringDivision().getAllNumerics("the encrypted form of a key 12 is 2323jjj4334
code"));

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"));
}
}

You might also like