10-06-2011, 12:45
|
|
|
חבר מתאריך: 10.06.11
הודעות: 34
|
|
עזרה בתרגיל ב-C#
אני מנסה לכתוב תוכנית שבה אני אכניס טקסט מסויים שיהיה מורכב ממילים, אם יש תווים כמו פסיק, נק', דולר או רווח אז זה אומר שמה שהיה עד עכשיו זו מילה ואז אני מתחיל מחדש לחפש את המילה הבאה ובסוף להציג את כל המילים בנפרד.
עכשיו התוכנית תתחיל לעבוד אחרי שאני מכניס טקסט ולוחץ פעמיים אנטר ז"א, אני יכול להכניס טקסט מסויים ואז באנטר להתחיל להכניס עוד טקסט בשורה חדשה אבל אם אני נמצא בשורה חדשה ולא מכניס כלום אלא לוחץ אנטר אז אני מקבל את המילים.
לדוגמא אם אני מכניס את הטקסט:
test abcd$rrr.tyu ver
asd dfg
אז אני מקבל את המילים מסודרות ככה:
test
abcd
rrr
tyu
ver
asd
dfg
התחלתי לכתוב משהו אבל אני מסתבך:
קוד:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PrintWordWordFromText
{
class Program
{
static void Main(string[] args)
{
//define temp
string temp = "";
int WordIndex = 0;
//read text from user
Console.Write("Enter Text: ");
string line = Console.ReadLine();
//define array
string[] Words = new string[100];
while (line.Length != 0)
{
for (int i = 0; i < line.Length; i++)
{
//We have a regular character - part of a word
if (line[i] != ' ' & line[i] != ' ' & line[i] != ',' & line[i] != '.' & line[i] != '$')
{
temp += line[i];
}
//special characters
else
{
Words[WordIndex] = temp;
WordIndex++;
temp = "";
}
}
}
for (int i = 0; i < WordIndex; i++)
{
Console.Write(Words[i]);
}
Console.ReadLine();
}
}
}
|