0% found this document useful (0 votes)
23 views6 pages

Generate A Random Numbers With A Specific Range

The document contains Java programs for generating random numbers within a specific range and performing various string manipulations. It includes functionalities such as finding string lengths, character retrieval, string concatenation, substring searching, and substring extraction. Each program utilizes user input and demonstrates basic operations on strings and random number generation.

Uploaded by

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

Generate A Random Numbers With A Specific Range

The document contains Java programs for generating random numbers within a specific range and performing various string manipulations. It includes functionalities such as finding string lengths, character retrieval, string concatenation, substring searching, and substring extraction. Each program utilizes user input and demonstrates basic operations on strings and random number generation.

Uploaded by

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

Generate a Random Numbers with a Specific Range

import java.io.*;

import java.util.*;

class GFG {

public static void main (String[] args) {

Random rand = new Random();

int max=100,min=50;

System.out.println("Generated numbers are within "+min+" to "+max);

System.out.println(rand.nextInt(max - min + 1) + min);

System.out.println(rand.nextInt(max - min + 1) + min);

System.out.println(rand.nextInt(max - min + 1) + min);

String Manipulation

1. Finding the length of a string.


2. Finding the character at a particular position.
3. Concatenating two strings.

import java.util.Scanner;

public class StringManipulation {

public static void main(String[] args) {

// Scanner to take user input

Scanner scanner = new Scanner(System.in);


// Accept two strings from the user

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

String str1 = scanner.nextLine();

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

String str2 = scanner.nextLine();

// Convert strings to character arrays

char[] charArray1 = str1.toCharArray();

char[] charArray2 = str2.toCharArray();

// 1. String Length

System.out.println("\nString Length:");

System.out.println("Length of first string: " + str1.length());

System.out.println("Length of second string: " + str2.length());

// 2. Finding character at a particular position

System.out.print("\nEnter a position to find the character (0-based index): ");

int position = scanner.nextInt();

if (position >= 0 && position < str1.length()) {

System.out.println("Character at position " + position + " in the first string: " + charArray1[position]);

} else {

System.out.println("Invalid position for the first string.");

if (position >= 0 && position < str2.length()) {


System.out.println("Character at position " + position + " in the second string: " + charArray2[position]);

} else {

System.out.println("Invalid position for the second string.");

// 3. Concatenating two strings

String concatenatedString = str1 + str2;

System.out.println("\nConcatenated String: " + concatenatedString);

// Close the scanner

scanner.close();

Write a program to perform the following string operations using string class a) string concatenation,
search a substring, to extract substring from given string

import java.util.Scanner;

public class StringOperations {

public static void main(String[] args) {

// Create a scanner object to take input

Scanner scanner = new Scanner(System.in);

// Accept the first string from the user

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

String str1 = scanner.nextLine();


// Accept the second string from the user

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

String str2 = scanner.nextLine();

// 1. String Concatenation

String concatenatedString = str1 + str2; // Concatenate using + operator

System.out.println("\nConcatenated String: " + concatenatedString);

// Alternatively, you can also use concat() method:

String concatMethod = str1.concat(str2);

System.out.println("Concatenated String using concat(): " + concatMethod);

// 2. Search for a substring in a string

System.out.print("\nEnter a substring to search: ");

String substringToSearch = scanner.nextLine();

if (str1.contains(substringToSearch)) {

System.out.println("The substring \"" + substringToSearch + "\" is found in the first string.");

} else {

System.out.println("The substring \"" + substringToSearch + "\" is NOT found in the first string.");

// You can also use indexOf() to get the position of the substring

int index = str1.indexOf(substringToSearch);

if (index != -1) {

System.out.println("The substring \"" + substringToSearch + "\" is found at position: " + index);

} else {
System.out.println("The substring \"" + substringToSearch + "\" is NOT found in the first string.");

// 3. Extract a substring from a given string

System.out.print("\nEnter the starting index for substring extraction: ");

int startIndex = scanner.nextInt();

System.out.print("Enter the ending index for substring extraction: ");

int endIndex = scanner.nextInt();

if (startIndex >= 0 && endIndex <= str1.length() && startIndex < endIndex) {

String extractedSubstring = str1.substring(startIndex, endIndex);

System.out.println("Extracted Substring: " + extractedSubstring);

} else {

System.out.println("Invalid indices for substring extraction.");

// Close the scanner

scanner.close();

You might also like