תוכן עניינים
1. מחרוזת אותיות גדולות לקטנות
2. מחרוזת אותיות גדולות לקטנות (ללא פעולות עזר)
3. האם מילה מופיעה במחרוזת
כיתבו תוכנית שתקלוט מהמשתמש שתי מחרוזות. האחת היא מילה והשנייה היא משפט. התוכנית תבדוק אם המילה מופיעה בתחילת המחרוזת ותדפיס הודעה בהתאם.
4. האם תו מופיע במחרוזת
כיתבו תוכנית שתקלוט מהמשתמש תו בודד char וכן תקלוט גם מחרוזת. התוכנית תדפיס True אם התו מופיע במחרוזת בין 1 ל-3 פעמים, אחרת יודפס False.
5. ספירת מספר האותיות הקטנות במחרוזת
כתבו תוכנית שתקלוט מהמשתמש מחרוזת. התוכנית תספור כמה אותיות קטנות (a–z) יש במחרוזת ותדפיס את הכמות.
6. האם כל האותיות במחרוזת הן גדולות
כתבו תוכנית שתקלוט מחרוזת ותבדוק האם כל התווים בה הן אותיות גדולות בלבד (A–Z). אם כן, התוכנית תדפיס True. אחרת תדפיס False.
7. הדפסת מחרוזת ללא רווחים
כתבו תוכנית שתקלוט מחרוזת ותדפיס אותה מחדש, אך בלי רווחים בכלל (לא רווחים בתחילה, באמצע או בסוף).
8. ספירת תווים מסוימים במחרוזת
כתבו תוכנית שתקלוט מחרוזת וכן תקלוט תו בודד. לאחר מכן, התוכנית תספור כמה פעמים התו מופיע במחרוזת ותדפיס את הכמות.
9. האם המחרוזת כוללת רק ספרות
כתבו תוכנית שתקלוט מחרוזת ותבדוק אם כל התווים בה הם ספרות בלבד (0–9). אם כן – תדפיס True, אחרת – False.
10. הדפסת מחרוזת במהופך
כתבו תוכנית שתקלוט מחרוזת ותדפיס אותה הפוך. לדוגמה: עבור הקלט hello
יודפס olleh
.
פתרונות
פתרון תרגיל 1
using System;
class Program
{
static void Main()
{
Console.Write("Enter uppercase string: ");
string input = Console.ReadLine();
for (int i = 0; i < input.Length; i++)
{
// Convert uppercase (A-Z) to lowercase (a-z)
if (input[i] >= 'A' && input[i] <= 'Z')
{
char lower = (char)(input[i] + 32);
Console.Write(lower);
}
else
{
Console.Write(input[i]); // keep as-is
}
}
Console.WriteLine();
}
}
פתרון תרגיל 2
using System;
class Program
{
static void Main()
{
Console.Write("Enter uppercase letters only: ");
string input = Console.ReadLine();
for (int i = 0; i < input.Length; i++)
{
// Assumes all letters are uppercase
char lower = (char)(input[i] + 32);
Console.Write(lower);
}
Console.WriteLine();
}
}
פתרון תרגיל 3
using System;
class Program
{
static void Main()
{
Console.Write("Enter word: ");
string word = Console.ReadLine();
Console.Write("Enter sentence: ");
string sentence = Console.ReadLine();
bool match = true;
if (word.Length > sentence.Length)
match = false;
else
{
for (int i = 0; i < word.Length; i++)
{
if (word[i] != sentence[i])
{
match = false;
break;
}
}
}
if (match)
Console.WriteLine("The word appears at the beginning.");
else
Console.WriteLine("The word does not appear at the beginning.");
}
}
פתרון תרגיל 4
using System;
class Program
{
static void Main()
{
Console.Write("Enter a character: ");
char ch = Console.ReadLine()[0];
Console.Write("Enter a string: ");
string text = Console.ReadLine();
int count = 0;
for (int i = 0; i < text.Length; i++)
{
if (text[i] == ch)
count++;
}
if (count >= 1 && count <= 3)
Console.WriteLine("True");
else
Console.WriteLine("False");
}
}
פתרון תרגיל 5
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
int count = 0;
for (int i = 0; i < input.Length; i++)
{
if (input[i] >= 'a' && input[i] <= 'z')
{
count++;
}
}
Console.WriteLine("Number of lowercase letters: " + count);
}
}
פתרון תרגיל 6
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
bool allUppercase = true;
for (int i = 0; i < input.Length; i++)
{
if (!(input[i] >= 'A' && input[i] <= 'Z'))
{
allUppercase = false;
break;
}
}
Console.WriteLine(allUppercase);
}
}
פתרון תרגיל 7
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
string result = "";
for (int i = 0; i < input.Length; i++)
{
if (input[i] != ' ')
{
result += input[i];
}
}
Console.WriteLine("String without spaces: " + result);
}
}
פתרון תרגיל 8
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
Console.Write("Enter a character: ");
char ch = Console.ReadLine()[0];
int count = 0;
for (int i = 0; i < input.Length; i++)
{
if (input[i] == ch)
{
count++;
}
}
Console.WriteLine("Character appears " + count + " times.");
}
}
פתרון תרגיל 9
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
bool onlyDigits = true;
for (int i = 0; i < input.Length; i++)
{
if (!(input[i] >= '0' && input[i] <= '9'))
{
onlyDigits = false;
break;
}
}
Console.WriteLine(onlyDigits);
}
}
פתרון תרגיל 10
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
string reversed = "";
for (int i = input.Length - 1; i >= 0; i--)
{
reversed += input[i];
}
Console.WriteLine("Reversed string: " + reversed);
}
}