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

Str_nextWord Procedure Write a procedure called Str_nextWord that scans a string

ID: 3868729 • Letter: S

Question

Str_nextWord Procedure Write a procedure called Str_nextWord that scans a string for th first occurrence of a certain delimiter character and replaces the delimiter with a null byte. There are two input parameters: a pointer to the string and the delimiter character. After the call, if the delimiter was found, the Zero flag is set and EAX contains the offset of the next character beyond the delimiter. Otherwise, the Zero flag is clear and EAX is undefined. The following example code passes the address of target and a comma as the delimiter: .data target BYTE "Johnson, Calvin", 0 .code INVOKE Str_nextWord, ADDR target, ',' jnz notFound

Explanation / Answer

Hello.
This program can be done with many different approaches in java.
I have used StringBuffer class here, because it provides many functionalities.


class StringDemo
{
public static void main(String args[])
{
StringBuffer sb;
boolean Zero;
int EAX;
public int Str_nextWord(String s,char ch)
{
sb=new StringBuffer(s);
int pos=sb.indexOf(ch);             //searches the first occurence of ch in sb
sb=sb.deleteCharAt(pos);         //deletes the character from the position pos
sb.setCharAt(pos,'');              //set the character at pos to null byte
return pos;
}

int p=Str_nextWord("Johnson,Calvin", ',');
if(p!= -1)
{
   Zero=true;
   EAX=p+1;
   System.out.println("delimeter comma is found and replaced with null byte in the given string");
}
else
{  
   Zero=false;
   EAX=NULL;
   System.out.println("delimeter comma is not found in the given string");
}
}
}