এই নিবন্ধে, আমরা কীভাবে এলোমেলো স্ট্রিং তৈরি করতে হয় তা বুঝব। স্ট্রিং একটি ডেটাটাইপ যা এক বা একাধিক অক্ষর ধারণ করে এবং ডবল উদ্ধৃতি (“ ”) দিয়ে আবদ্ধ থাকে।
নীচে একই -
এর একটি প্রদর্শন রয়েছে৷ধরুন আমাদের ইনপুট হল −
The size of the string is defined as: 10
কাঙ্খিত আউটপুট হবে −
Random string: ink1n1dodv
অ্যালগরিদম
Step 1 - START Step 2 - Declare an integer namely string_size, a string namely alpha_numeric and an object of StringBuilder namely string_builder. Step 3 - Define the values. Step 4 - Iterate for 10 times usinf a for-loop, generate a random value using the function Math.random() and append the value using append() function. Step 5 - Display the result Step 6 - Stop
উদাহরণ 1
এখানে, আমরা 'প্রধান' ফাংশনের অধীনে সমস্ত ক্রিয়াকলাপ একসাথে আবদ্ধ করি।
public class RandomString {
public static void main(String[] args) {
int string_size = 10;
System.out.println("The size of the string is defined as: " +string_size);
String alpha_numeric = "0123456789" + "abcdefghijklmnopqrstuvxyz";
StringBuilder string_builder = new StringBuilder(string_size);
for (int i = 0; i < string_size; i++) {
int index = (int)(alpha_numeric.length() * Math.random());
string_builder.append(alpha_numeric.charAt(index));
}
String result = string_builder.toString();
System.out.println("The random string generated is: " +result);
}
} আউটপুট
The size of the string is defined as: 10 The random string generated is: ink1n1dodv
উদাহরণ 2
এখানে, আমরা ক্রিয়াকলাপগুলিকে অবজেক্ট-ওরিয়েন্টেড প্রোগ্রামিং প্রদর্শনকারী ফাংশনে অন্তর্ভুক্ত করি।
public class RandomString {
static String getAlphaNumericString(int string_size) {
String alpha_numeric = "0123456789" + "abcdefghijklmnopqrstuvxyz";
StringBuilder string_builder = new StringBuilder(string_size);
for (int i = 0; i < string_size; i++) {
int index = (int)(alpha_numeric.length() * Math.random());
string_builder.append(alpha_numeric.charAt(index));
}
return string_builder.toString();
}
public static void main(String[] args) {
int string_size = 10;
System.out.println("The size of the string is defined as: " +string_size);
System.out.println("The random string generated is: ");
System.out.println(RandomString.getAlphaNumericString(string_size));
}
} আউটপুট
The size of the string is defined as: 10 The random string generated is: ink1n1dodv