تحديات برمجيةالتحدي الثاني - حل التمرين الرابع بلغة جافا
المطلوب
قم بإنشاء برنامج يطلب من المستخدم إدخال ثلاث نصوص و يخزنهم في ثلاث متغيرات نصية هي S1
, S2
و S3
و بعدها ينفذ التالي:
- يخبره ما إن كان دمج
S1
معS2
يساويS3
أم لا. - يخبره ما إن كان
S1
يمثل جزء منS2
أو يساويه ( أيS1 == s2
). - إذا كان طول
S1
أكبر من طولS2
قم بإضافة نصS2
علىS1
و خزن الناتج في متغير نصي جديد إسمهS4
. - إذا كان طول
S2
أكبر من طولS1
قم بإضافة نصS1
علىS2
و خزن الناتج في متغير نصي جديد إسمهS4
. - يخبره ما إن كان
S2
يمثل جزء من ثاني نصف فيS1
. - يعرض له الأحرف الموجودة في أول نصف في
S1
.
الحل بلغة جافا
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String S1, S2, S3, S4="";
System.out.print("Enter S1: ");
S1 = input.nextLine();
System.out.print("Enter S2: ");
S2 = input.nextLine();
System.out.print("Enter S3: ");
S3 = input.nextLine();
System.out.println("---------------------------------------");
// Part 1
System.out.println("The concatenation of S1 and S2 is equal S3? " + S3.equals(S1 + S2));
// Part 2
System.out.println("S1 is part of S2 or S1=S2? " + S2.contains(S1));
// Part 3
if (S1.length() > S2.length()) {
S4 = S2 + S1;
}
// Part 4
if (S2.length() > S1.length()) {
S4 = S1 + S2;
}
// Part 5
System.out.println("S2 is part of the second half of S1? " + (S1.indexOf(S2, S1.length() / 2) != -1));
// Part 6
System.out.print("The first half characters of S1 are: ");
for (int i = 0; i < S1.length() / 2; i++) {
System.out.print(S1.charAt(i));
}
System.out.println();
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String S1, S2, S3, S4="";
System.out.print("Enter S1: ");
S1 = input.nextLine();
System.out.print("Enter S2: ");
S2 = input.nextLine();
System.out.print("Enter S3: ");
S3 = input.nextLine();
System.out.println("---------------------------------------");
// Part 1
System.out.println("The concatenation of S1 and S2 is equal S3? " + S3.equals(S1 + S2));
// Part 2
System.out.println("S1 is part of S2 or S1=S2? " + S2.contains(S1));
// Part 3
if (S1.length() > S2.length()) {
S4 = S2 + S1;
}
// Part 4
if (S2.length() > S1.length()) {
S4 = S1 + S2;
}
// Part 5
System.out.println("S2 is part of the second half of S1? " + (S1.indexOf(S2, S1.length() / 2) != -1));
// Part 6
System.out.print("The first half characters of S1 are: ");
for (int i = 0; i < S1.length() / 2; i++) {
System.out.print(S1.charAt(i));
}
System.out.println();
}
}
سنحصل على النتيجة التالية في حال تم إدخال نفس القيم التي تم تعليمها باللون الأصفر عند التشغيل.
Enter S1: Mhamad
Enter S2: Harmush
Enter S3: MhamadHarmush
---------------------------------------
The concatenation of S1 and S2 is equal S3? true
S1 is part of S2 or S1=S2? false
S2 is part of the second half of S1? false
The first half characters of S1 are: Mha
Enter S2: Harmush
Enter S3: MhamadHarmush
---------------------------------------
The concatenation of S1 and S2 is equal S3? true
S1 is part of S2 or S1=S2? false
S2 is part of the second half of S1? false
The first half characters of S1 are: Mha