Strings and nested loops · السلاسل والحلقات المتداخلة
Strings and nested loops
- A
Stringis a row of characters, each with a position (an index). - The first index is
0, and the last islength() - 1. - In this lesson you walk through a String one character at a time, and put loops inside loops.
السلاسل والحلقات المتداخلة
Stringهو صف من الأحرف، لكل منها موقع (فهرس).- الفهرس الأول هو
0، والأخير هوlength() - 1. - في هذا الدرس ستمر عبر السلسلة حرفاً تلو الآخر، وتضع حلقات داخل حلقات.
Traversing a String
s.length()gives the number of characters.s.charAt(i)gives the character at indexi.- Loop
ifrom0up tolength() - 1to visit every character.
المرور على السلسلة
s.length()يعطي عدد الأحرف.s.charAt(i)يعطي الحرف عند الفهرسi.- حلقة
iمن0حتىlength() - 1لزيارة كل حرف.
public class Main {
public static void main(String[] args) {
String s = "code";
for (int i = 0; i < s.length(); i++) {
System.out.println(s.charAt(i));
}
}
}
Counting characters
- You can count how many times a character appears.
- Use a counter that starts at
0. - A single character uses single quotes, like
'o'. Compare with==(chars, not Strings).
عد الأحرف
- يمكنك عد المرات التي يظهر فيها حرف معين.
- استخدم عداداً يبدأ بـ
0. - الحرف المفرد يستخدم علامات اقتباس مفردة، مثل
'o'. قارنه بـ==(حروف، وليس سلاسل).
public class Main {
public static void main(String[] args) {
String s = "balloon";
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'o') {
count++;
}
}
System.out.println(count); // 2
}
}
Building a new String
- Start with an empty String
""and add characters with+. - Reading from the end to the start reverses the String.
- Each
+makes a longer String.
بناء سلسلة جديدة
- ابدأ بسلسلة فارغة
""وأضف أحرفاً باستخدام+. - القراءة من النهاية إلى البداية تعكس السلسلة.
- كل
+يجعل سلسلة أطول.
public class Main {
public static void main(String[] args) {
String s = "code";
String result = "";
for (int i = s.length() - 1; i >= 0; i--) {
result = result + s.charAt(i);
}
System.out.println(result); // edoc
}
}
Enhanced for (preview)
- For walking a String you usually need the index, so a normal
foris best. - But Java also has an enhanced for loop that visits items directly.
- You will use it a lot with arrays in lesson 8. Here is a small preview with text.
حلقة for المحسنة (مقدمة)
- للمرور على السلسلة通常需要 الفهرس، لذا فإن
forالعادية هي الأفضل. - ولكن Java لديها أيضاً حلقة for المحسنة التي تزور العناصر مباشرة.
- ستستخدمها كثيراً مع المصفوفات في الدرس 8. إليك مقدمة صغيرة مع النصوص.
public class Main {
public static void main(String[] args) {
String[] words = {"hi", "there", "!"};
for (String word : words) {
System.out.println(word);
}
}
}
Nested loops
- A nested loop is a loop inside another loop.
- The inner loop runs all the way for each step of the outer loop.
- Below, the outer loop picks a row, and the inner loop prints stars in that row.
الحلقات المتداخلة
- الحلقة المتداخلة هي حلقة داخل حلقة أخرى.
- تعمل الحلقة الداخلية كاملة من أجل كل خطوة للحلقة الخارجية.
- أدناه، تختار الحلقة الخارجية صفاً، وتطبع الحلقة الداخلية نجومات في ذلك الصف.
public class Main {
public static void main(String[] args) {
for (int row = 1; row <= 3; row++) {
String line = "";
for (int col = 1; col <= row; col++) {
line = line + "*";
}
System.out.println(line);
}
}
}
Common mistakes
- Strings are immutable — store the result of
s.substring(...)etc. s.length()is a method (with()), unlike an array's.length.
أخطاء شائعة
- السلاسل النصية لا يمكن تعديلها — احفظ نتيجة
s.substring(...)وما شابه. s.length()هي دالة (مع())، على عكس.lengthالخاص بالمصفوفة.
Now you try
- Each task pre-fills the class skeleton — write your code inside main, or complete the method shown.
- Press Run to compile and run, then Check answer.
- Your code compiles and runs on the server, so even the first run is fast.
الآن جرب بنفسك
- كل مهمة تملأ هيكل الفئة مسبقاً — اكتب كودك داخل main، أو أكمل الدالة المعروضة.
- اضغط تشغيل للمعالجة والتشغيل، ثم تحقق من الإجابة.
- كودك يُعالج ويُشغل على الخادم، لذا حتى التشغيل الأول سريع.
Indexing a String · الفهرسة في String
Each char has an index from 0; substring takes a range. · لكل حرف فهرس من 0؛ substring يأخذ نطاقاً.
Complete countChar(String s, char c) so it returns how many times c appears in s. Use charAt and ==. For example countChar("balloon", 'o') is 2. · أكمل countChar(String s, char c) لتُرجع كم مرة يظهر c في s. استخدم charAt و==. على سبيل المثال countChar("balloon", 'o') هو 2.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete reverse(String s) so it returns s with the letters in the opposite order. For example reverse("code") is "edoc". Build a new String with +. · أكمل reverse(String s) لتُرجع s مع الأحرف في الترتيب المعاكس. على سبيل المثال reverse("code") هي "edoc". بنِ String جديد باستخدام +.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Use nested loops to print a triangle of stars: line 1 has one *, line 2 has two, line 3 has three, line 4 has four. Print each line with System.out.println. · استخدم حلقات متداخلة لطباعة مثلث من النجوم: السطر 1 يحتوي على نجمة * واحدة، السطر 2 يحتوي على اثنتين، السطر 3 يحتوي على ثلاثة، السطر 4 يحتوي على أربعة. اطبع كل سطر مع System.out.println.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.