Generate a Random Numbers with a Specific Range
import [Link].*;
import [Link].*;
class GFG {
public static void main (String[] args) {
Random rand = new Random();
int max=100,min=50;
[Link]("Generated numbers are within "+min+" to "+max);
[Link]([Link](max - min + 1) + min);
[Link]([Link](max - min + 1) + min);
[Link]([Link](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 [Link];
public class StringManipulation {
public static void main(String[] args) {
// Scanner to take user input
Scanner scanner = new Scanner([Link]);
// Accept two strings from the user
[Link]("Enter the first string:");
String str1 = [Link]();
[Link]("Enter the second string:");
String str2 = [Link]();
// Convert strings to character arrays
char[] charArray1 = [Link]();
char[] charArray2 = [Link]();
// 1. String Length
[Link]("\nString Length:");
[Link]("Length of first string: " + [Link]());
[Link]("Length of second string: " + [Link]());
// 2. Finding character at a particular position
[Link]("\nEnter a position to find the character (0-based index): ");
int position = [Link]();
if (position >= 0 && position < [Link]()) {
[Link]("Character at position " + position + " in the first string: " + charArray1[position]);
} else {
[Link]("Invalid position for the first string.");
if (position >= 0 && position < [Link]()) {
[Link]("Character at position " + position + " in the second string: " + charArray2[position]);
} else {
[Link]("Invalid position for the second string.");
// 3. Concatenating two strings
String concatenatedString = str1 + str2;
[Link]("\nConcatenated String: " + concatenatedString);
// Close the scanner
[Link]();
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 [Link];
public class StringOperations {
public static void main(String[] args) {
// Create a scanner object to take input
Scanner scanner = new Scanner([Link]);
// Accept the first string from the user
[Link]("Enter the first string:");
String str1 = [Link]();
// Accept the second string from the user
[Link]("Enter the second string:");
String str2 = [Link]();
// 1. String Concatenation
String concatenatedString = str1 + str2; // Concatenate using + operator
[Link]("\nConcatenated String: " + concatenatedString);
// Alternatively, you can also use concat() method:
String concatMethod = [Link](str2);
[Link]("Concatenated String using concat(): " + concatMethod);
// 2. Search for a substring in a string
[Link]("\nEnter a substring to search: ");
String substringToSearch = [Link]();
if ([Link](substringToSearch)) {
[Link]("The substring \"" + substringToSearch + "\" is found in the first string.");
} else {
[Link]("The substring \"" + substringToSearch + "\" is NOT found in the first string.");
// You can also use indexOf() to get the position of the substring
int index = [Link](substringToSearch);
if (index != -1) {
[Link]("The substring \"" + substringToSearch + "\" is found at position: " + index);
} else {
[Link]("The substring \"" + substringToSearch + "\" is NOT found in the first string.");
// 3. Extract a substring from a given string
[Link]("\nEnter the starting index for substring extraction: ");
int startIndex = [Link]();
[Link]("Enter the ending index for substring extraction: ");
int endIndex = [Link]();
if (startIndex >= 0 && endIndex <= [Link]() && startIndex < endIndex) {
String extractedSubstring = [Link](startIndex, endIndex);
[Link]("Extracted Substring: " + extractedSubstring);
} else {
[Link]("Invalid indices for substring extraction.");
// Close the scanner
[Link]();