المطلوب
قم بإنشاء برنامج يطلب من المستخدم إدخال نص و من ثم إدخل حرف واحد.
بعدها سيقوم البرنامج بطباعة مكان كل ( Index ) موجود عليه هذا الحرف في النص.
مثال: إذا قام المستخدم بإدخال النص "Harmash is the best site to learn programming" و من ثم أدخل الحرف "a" فيجب أن يطبع له النتيجة التالية عند التشغيل.
'a' found at index: 1 'a' found at index: 4 'a' found at index: 30 'a' found at index: 39
الحل بلغة جافا
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String text;
char character;
System.out.print("Enter any text: ");
text = input.nextLine();
System.out.print("Enter a character: ");
character = input.nextLine().charAt(0);
for (int i = 0; i < text.length(); i++)
{
if (text.charAt(i) == character)
{
System.out.println("'" + character + "' found at index: " + i);
}
}
}
}
سنحصل على النتيجة التالية في حال تم إدخال نفس القيم التي تم تعليمها باللون الأصفر عند التشغيل.
Enter any text: Harmash is the best site to learn programming
Enter a character: a
'a' found at index: 1
'a' found at index: 4
'a' found at index: 30
'a' found at index: 39
Enter a character: a
'a' found at index: 1
'a' found at index: 4
'a' found at index: 30
'a' found at index: 39


محرر الويب
نظام الألوان
محول الوحدات
محلل عناوين الشبكات