0% found this document useful (0 votes)
9 views3 pages

Individual Assigment

assingment

Uploaded by

tnyange909
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Individual Assigment

assingment

Uploaded by

tnyange909
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

(a) Character as a Data Structure

A character in programming refers to a single symbol, letter, digit, or any other textual element that
is typically represented in a character encoding scheme such as ASCII or Unicode. As a data
structure, a character can be considered a primitive data type or part of a larger composite structure,
like strings or character arrays.
In terms of storage:
• A character is stored as a fixed-sized unit (e.g., 1 byte in ASCII, 2 bytes in UTF-16 for Java).
• It can hold individual characters such as 'A', '1', or symbols like '@'.

Characters in an array can serve as a basic building block for strings or perform specific operations
like searching, insertion, and deletion in text processing tasks.

(b) Operations on Characters in an Array Using Java

Explanation of Operations
• Insertion:
Adds a new character at the specified index, shifting subsequent elements to the right.
Java code to show insertion of characters in array

public class CharacterInsertion {


public static void main(String[] args) {
char[] charArray = {'A', 'B', 'C', 'D', 'E'};
char newChar = 'F';
int position = 2; // Insert at index 2

char[] updatedArray = insertCharacter(charArray, newChar, position);


System.out.println("After Insertion: " + String.valueOf(updatedArray));
}

public static char[] insertCharacter(char[] array, char newChar, int position) {


char[] newArray = new char[array.length + 1];
for (int i = 0, j = 0; i < newArray.length; i++) {
if (i == position) {
newArray[i] = newChar; // Insert new character
} else {
newArray[i] = array[j++]; // Copy elements from old array
}
}
return newArray;
}
}
• Search:
Iterates through the array to locate a target character, returning its index.

Java code to show search of characters in array

public class CharacterSearch {


public static void main(String[] args) {
char[] charArray = {'A', 'B', 'C', 'D', 'E'};
char target = 'C'; // Character to search

int index = searchCharacter(charArray, target);


if (index != -1) {
System.out.println("'" + target + "' found at index: " + index);
} else {
System.out.println("'" + target + "' not found in the array.");
}
}

public static int searchCharacter(char[] array, char target) {


for (int i = 0; i < array.length; i++) {
if (array[i] == target) {
return i; // Found
}
}
return -1; // Not found
}
}

• Deletion:
Removes a character from the array, creating a new array with one less element and skipping
the target index.
• Java code to show deletion of characters in array

public class CharacterDeletion {


public static void main(String[] args) {
char[] charArray = {'A', 'B', 'C', 'D', 'E'};
int position = 3; // Delete the character at index 3
char[] updatedArray = deleteCharacter(charArray, position);
System.out.println("After Deletion: " + String.valueOf(updatedArray));
}

public static char[] deleteCharacter(char[] array, int position) {


if (position < 0 || position >= array.length) {
System.out.println("Invalid position");
return array; // Return original array if position is invalid
}
char[] newArray = new char[array.length - 1];
for (int i = 0, j = 0; i < array.length; i++) {
if (i != position) {
// Copy all except the deleted element
newArray[j++] = array[i];
} }
return newArray;
}
}

You might also like