0% found this document useful (0 votes)
17 views7 pages

Java Programs for Array and String Manipulation

The document contains code snippets that demonstrate how to perform various tasks in Java including: 1) Using linear search to search an array for a user-input integer and print whether it was found or not 2) Counting and printing the number of words starting with "A" in a user-input sentence 3) Converting the first letter of each word in a lowercase user-input string to uppercase 4) Accepting student names and marks into arrays, calculating average marks and deviation from average 5) Inputting integers into an array, finding the largest, smallest, and sum

Uploaded by

Advaith Vijesh
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)
17 views7 pages

Java Programs for Array and String Manipulation

The document contains code snippets that demonstrate how to perform various tasks in Java including: 1) Using linear search to search an array for a user-input integer and print whether it was found or not 2) Counting and printing the number of words starting with "A" in a user-input sentence 3) Converting the first letter of each word in a lowercase user-input string to uppercase 4) Accepting student names and marks into arrays, calculating average marks and deviation from average 5) Inputting integers into an array, finding the largest, smallest, and sum

Uploaded by

Advaith Vijesh
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

Question 5

Write a program to search for an integer value input by the user in the list given
below using the linear search technique. If found display "Search Successful"
and print the index of the element in the array, otherwise display "Search
Unsuccessful".

{75, 86, 90, 45, 31, 50, 36, 60, 12, 47}

iAnswer

import [Link];

public class KboatLinearSearch


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);

int arr[] = {75, 86, 90, 45, 31, 50, 36, 60, 12, 47};
int l = [Link];
int i = 0;

[Link]("Enter the number to search: ");


int n = [Link]();

for (i = 0; i < l; i++) {


if (arr[i] == n) {
break;
}
}

if (i == l) {
[Link]("Search Unsuccessful");
}
else {
[Link]("Search Successful");
[Link](n + " present at index " + i);
}
}
Write a program to input a sentence and convert it into uppercase and count and
display the total number of words starting with a letter 'A'.

Example:

Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION


TECHNOLOGY ARE EVER CHANGING.

Sample Output: Total number of words starting with letter 'A' = 4

Answer

import [Link];

public class WordsWithLetterA


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
str = " " + str; //Add space in the begining of str
int c = 0;
int len = [Link]();
str = [Link]();
for (int i = 0; i < len - 1; i++) {
if ([Link](i) == ' ' && [Link](i + 1) == 'A')
c++;
}
[Link]("Total number of words starting with letter 'A' = " + c);
}
}

Write a program in Java to accept a string in lower case and change the first letter
of every word to upper case. Display the new string.

Sample input: we are in cyber world


Sample output: We Are In Cyber World

Answer

import [Link];

public class KboatString

public static void main(String args[]) {

Scanner in = new Scanner([Link]);

[Link]("Enter a sentence:");

String str = [Link]();

String word = "";

for (int i = 0; i < [Link](); i++) {

if (i == 0 || [Link](i - 1) == ' ') {

word += [Link]([Link](i));

else {

word += [Link](i);

}
[Link](word);

rite a program to accept name and total marks of N number of students in two
single subscript array name[] and totalmarks[].

Calculate and print:

1. The average of the total marks obtained by N number of students.


[average = (sum of total marks of all the students)/N]
2. Deviation of each student’s total marks with the average.
[deviation = total marks of a student – average]

Answer

import [Link];

public class KboatSDAMarks

public static void main(String args[]) {

Scanner in = new Scanner([Link]);

[Link]("Enter number of students: ");

int n = [Link]();

String name[] = new String[n];


int totalmarks[] = new int[n];

int grandTotal = 0;

for (int i = 0; i < n; i++) {

[Link]();

[Link]("Enter name of student " + (i+1) + ": ");

name[i] = [Link]();

[Link]("Enter total marks of student " + (i+1) + ": ");

totalmarks[i] = [Link]();

grandTotal += totalmarks[i];

double avg = grandTotal / (double)n;

[Link]("Average = " + avg);

for (int i = 0; i < n; i++) {

[Link]("Deviation for " + name[i] + " = "

+ (totalmarks[i] - avg));

}
}

Write a program to input integer elements into an array of size 20 and perform the
following operations:

1. Display largest number from the array.


2. Display smallest number from the array.
3. Display sum of all the elements of the array

Answer

import [Link];

public class KboatSDAMinMaxSum

public static void main(String args[]) {

Scanner in = new Scanner([Link]);

int arr[] = new int[20];

[Link]("Enter 20 numbers:");

for (int i = 0; i < 20; i++) {

arr[i] = [Link]();

int min = arr[0], max = arr[0], sum = 0;

for (int i = 0; i < [Link]; i++) {

if (arr[i] < min)


min = arr[i];

if (arr[i] > max)

max = arr[i];

sum += arr[i];

[Link]("Largest Number = " + max);

[Link]("Smallest Number = " + min);

[Link]("Sum = " + sum);

Common questions

Powered by AI

To calculate and print the average and deviation of total marks, first collect total marks in an array and compute their sum. The average is calculated by dividing this sum by the number of students. The deviation for each student is then calculated by subtracting the average from their total marks. This involves storage of names and marks in arrays, iteration to compute the sum, and further iteration to compute deviations .

The program uses a `Scanner` to input names and marks into arrays. It calculates the sum of all marks to determine the average. Deviations are calculated by subtracting the average from each student's marks. This involves loops for inputting data, calculating the sum, and computing deviations, highlighting the use of arrays to manage related data efficiently .

The program initializes `min` and `max` with the first array element and iterates through the array, updating `min` if a smaller element is found and `max` if a larger one is found. This ensures that by the end of the loop, `min` holds the smallest and `max` holds the largest number in the array, allowing their display .

Adding a space at the beginning of the string ensures consistent detection of words starting with a specific letter by allowing the check for a space followed by the letter at the beginning of the string. This technique simplifies the logic by treating the sentence start as a word boundary akin to spaces within the sentence .

The program collects marks into an array and uses a loop to compute the total. The average is derived by dividing the total by the number of students. The computational complexity is O(n) due to the iteration through the marks to compute the sum, reflecting the operation's linear nature with respect to input size .

The program adds a space at the start of the sentence and converts it to uppercase, allowing it to detect a word starting by checking for a space followed by the target letter. Converting to uppercase standardizes the text case, enabling consistent identification of target letters regardless of their original case, which is crucial for accurate counting .

The algorithm involves iterating over each character in the string. If a character is the first one or follows a space, it is converted to uppercase using the `Character.toUpperCase()` method; otherwise, it remains unchanged. This approach effectively capitalizes the first letter of each word while leaving others in their original case, suitable for strings initially in lowercase .

The method involves converting the input sentence to uppercase, then using a loop to iterate over the characters. A counter is incremented whenever a space followed by an 'A' is found, indicating the start of a new word with 'A'. This method effectively counts the number of words starting with 'A' by checking consecutive characters in the string .

The linear search technique in Java can be implemented by iterating through each element in the array and comparing it to the target integer. If the integer is found, the program prints "Search Successful" along with the index of the element. If not found, it prints "Search Unsuccessful". The key is a loop that checks each element sequentially and a condition to break the loop if a match is found .

Java's `Character.toUpperCase()` is used in combination with manual iteration to selectively change the first character of each word in a string while leaving others unaltered. Manual iteration provides fine-grained control, necessary for operations like detecting word boundaries and conditionally modifying characters, which standard string functions don't automatically handle .

You might also like