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

write java in netbeans, operations on character arrays/lists or strings and not

ID: 3596384 • Letter: W

Question

write java in netbeans, operations on character arrays/lists or strings and not rely on any built-in string library or function such as string.h or STL. You must duplicate the operations of all five of the string functions in C++ : strlen: Accepts a C-string or a pointer to a string as an argument. Returns the length of the string (not including the null terminator. Example Usage: len = strlen(name); strcat: Accepts two C-strings or pointers to two strings as arguments. The function appends the contents of the second string to the first string. (The first string is altered, the second string is left unchanged.) Example Usage: strcat(string1, string2); strcpy: Accepts two C-strings or pointers to two strings as arguments. The function copies the second string to the first string. The second string is left unchanged. Example Usage: strcpy(string1, string2); strncpy: Accepts two C-strings or pointers to two strings and an integer argument. The third argument, an integer, indicates how many characters to copy from the second string to the first string. If the string2 has fewer than n characters, string1 is padded with '' characters. Example Usage: strncpy(string1, string2, n); strcmp: Accepts two C-strings or pointers to two string arguments. If string1 and string2are the same, this function returns 0. If string2 is alphabetically greater than string1, it returns a negative number. If string2 is alphabetically less than string1, it returns a positive number. Example Usage: if (strcmp(string1, string2)) strstr: Accepts two C-strings or pointers to two C-strings as arguments, searches for the first occurrence of string2 in string1. If an occurrence of string2 is found, the function returns a pointer to it. Otherwise, it returns a NULL pointer (address 0). Example Usage: cout << strstr(string1, string2);

Explanation / Answer

import java.util.*;

//Class StringOperations definition

public class StringOperations

{

//Declares character array

static char string1[], string2[], string3[];

//Method to simulate strlen()

static int strlen(char data[])

{

int len;

//Loops till end of data

for(len = 0; len < data.length; len++)

; //Do nothing statement

//Returns length

return len;

}//End of method

static char [] strcat(char data1[], char data2[])

{

int c, d;

//Adds the length of data1 and data2

int len = data1.length + data2.length;

//Creates an temporary arry of size data1 length plus data2 length

char concatData[] = new char[len+1];

//Loops till end of data1

for(c = 0; c < data1.length; c++)

//Stores each character of data1 in temporary array

concatData[c] = data1[c];

//Stores space at the end of the temporary array

concatData[c] = ' ';

//Starts from data1 length and loops till end of data2

for(c = data1.length+1, d = 0; d < data2.length; c++, d++)

//Stores each character of data2 in temporary array

concatData[c] = data2[d];

string1 = concatData;

//Returns the temporary array

return concatData;

}//End of method

//Method to simulate strcpy()

static char [] strcpy(char []data1, char []data2)

{

//Resize the data one length to data2 length

data1 = new char[data2.length];

//Loops till end of data2

for(int x = 0; x < data2.length; x++)

//Stores each character of data2 in data1

data1[x] = data2[x];

string1 = data1;

//Returns data1

return data1;

}//End of method

//Method to simulate strncpy()

static char[] strncpy(char []data1, char data2[], int n)

{

int c;

//Resize the data1 to n

data1 = new char[n];

//Checks if n is greater than data2 length

if(n > data2.length)

//Reset the n to data2 length

n = data2.length;

//Loops till m

for(c = 0; c < n; c++)

//Stores each character of data2 in data1

data1[c] = data2[c];

//Checks if data2 length is less than n

if(data2.length < n)

{

//Starts from data1 length and loops up to n

for(c = data1.length; c < n; c++)

//Padding space

data1[c] = ' ';

}//End of if

string1 = data1;

//Returns data1

return data1;

}//End of method

//Method to simulate strcmp()

static int strcmp(char []data1, char [] data2)

{

int res = 0;

//Checks if data1 length is greater than data2 length then assign 1 to result

if(data1.length > data2.length)

res = 1;

//Checks if data1 length is less than data2 length then assign -11 to result

else if(data1.length < data2.length)

res = -1;

//Otherwise length are same

else

{

//Loops either end of data1 length or data2 length

for(int c = 0; c < data1.length && c < data2.length; c++)

{

//Compares if data1 current position character is less than data2 current position character then assign -1 result

if(data1[c] < data2[c])

res = -1;

//Compares if data1 current position character is greater than data2 current position character then assign 1 result

else if(data1[c] > data2[c])

res = 1;

}//End of for

}//End of else

//Return the result

return res;

}//End of method

public static void main(String[] args)

{

//Scanner class object created to accept data

Scanner sc = new Scanner(System.in);

int n;

//Accepts data and converts it to character array

System.out.println(" Enter a string to calculate length: ");

string1 = sc.nextLine().toCharArray();

System.out.println("Length of the string: " + strlen(string1));

//strcat(string1, string2) operation

System.out.println(" strcat operation: ");

System.out.println(" Enter the first string: ");

string1 = sc.nextLine().toCharArray();

System.out.println(" Enter the second string: ");

string2 = sc.nextLine().toCharArray();

System.out.print(" First String: " );

for(int x = 0; x < string1.length; x++)

System.out.print(string1[x]);

System.out.print(" Second String: " );

for(int x = 0; x < string2.length; x++)

System.out.print(string2[x]);

strcat(string1, string2);

System.out.print(" After Concatenation First String: ");

for(int x = 0; x < string1.length; x++)

System.out.print(string1[x]);

//strcpy(string1, string2) operation

System.out.println(" strcpy operation: ");

System.out.println(" Enter the first string: ");

string1 = sc.nextLine().toCharArray();

System.out.println(" Enter the second string: ");

string2 = sc.nextLine().toCharArray();

System.out.print(" First String: " );

for(int x = 0; x < string1.length; x++)

System.out.print(string1[x]);

System.out.print(" Second String: " );

for(int x = 0; x < string2.length; x++)

System.out.print(string2[x]);

strcpy(string1, string2);

System.out.print(" After Copy First String: ");

for(int x = 0; x < string1.length; x++)

System.out.print(string1[x]);

System.out.println(" strcat operation with return result: ");

string3 = strcpy(string1, string2);

System.out.print(" After Copy First String: ");

for(int x = 0; x < string1.length; x++)

System.out.print(string3[x]);

//strncpy(string1, string2, n) operation

System.out.println(" strncat operation: ");

System.out.println(" Enter the first string: ");

string1 = sc.nextLine().toCharArray();

System.out.println(" Enter the second string: ");

string2 = sc.nextLine().toCharArray();

System.out.println(" Enter the position: ");

n = sc.nextInt();

System.out.print(" First String: " );

for(int x = 0; x < string1.length; x++)

System.out.print(string1[x]);

System.out.print(" Second String: " );

for(int x = 0; x < string2.length; x++)

System.out.print(string2[x]);

strncpy(string1, string2, n);

System.out.print(" After strncpy() First String: ");

for(int x = 0; x < string1.length; x++)

System.out.print(string1[x]);

System.out.println(" strncat operation with return result: ");

string3 = strncpy(string1, string2, n);

System.out.print(" After strncpy() First String: ");

for(int x = 0; x < string3.length; x++)

System.out.print(string3[x]);

sc.close();

}//End of main method

}//End of class

Sample Run:

Enter a string to calculate length:
this is demo.
Length of the string: 13

strcat operation:
Enter the first string:
check this
Enter the second string:
try it

First String: check this
Second String: try it
After Concatenation First String: check this try it
strcpy operation:
Enter the first string:
my name is
Enter the second string:
Pyari mohan

First String: my name is
Second String: Pyari mohan
After Copy First String: Pyari mohan
strcat operation with return result:

After Copy First String: Pyari mohan
strncat operation:

Enter the first string:
this is
Enter the second string:
demo
Enter the position:
3

First String: this is
Second String: demo
After strncpy() First String: dem
strncat operation with return result:

After strncpy() First String: dem