Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

strPtr(s1) This function will take a string which has two single-digit integers

ID: 3677773 • Letter: S

Question

strPtr(s1)

This function will take a string which has two single-digit integers in it. The first integer found is the starting point. The second integer is the substring length. The output from this function is a return of a substring of the string parameter with numbers removed, which begins at the starting point and returns a string the length indicated by the substring length integer.

Output must be presented in the format identical to that in the check function at the bottom of the template.

If an empty string is passed in, the following string will be returned in identical format to the one shown:

"Empty string"

If the integers passed in as starting and length values are invalid when applied to the string (after the numbers have been removed from it, the following string will be returned in identical format to the one shown:

“Invalid indices"

Output MUST BE as presented in the EXACT format used in the call to the check function at the bottom of the template.

SAMPLE RUNS strPtr('')

will return this string in the following format:

"Empty string"

strPtr('a39abcdefghiljklmnopa')

will return this string in the following format:

'cdefghilj'

strPtr('a27')

will return this string in the following format:

"Invalid indices"

Explanation / Answer

java program:


import java.io.*;
public class StringOperation {
public static void main(String[] args)throws IOException
{
StringOperation.strPtr("");
StringOperation.strPtr("a39abcdefghiljklmnopa");
StringOperation.strPtr("a27");
}
public static void strPtr(String str)
{int []a=new int[2];
char[] str1=new char[str.length()];
int j=0,k=0;
  
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)>=48 && str.charAt(i)<=57)
{
a[j]=Integer.parseInt(str.charAt(i)+"");
j++;
}
else
{
str1[k]=str.charAt(i);
k++;
}   
}
String s=new String(str1);
if(str.equalsIgnoreCase(""))
System.out.println("Empty String");
else if(str.length()<(a[0]+a[1]+2))
System.out.println("invalid indicies");
else
System.out.println(s.substring(a[0], a[0]+a[1]));
  
}
}

output:

run:
Empty String
cdefghilj
invalid indicies
BUILD SUCCESSFUL (total time: 0 seconds)