کلاسی با نام text manager که شامل متدهای زیر باشد
wordcount برای شمارش تعداد کلمات رشته ( wname)
space count تعداد space ها را می شمارد(sname)
paragraphcount تعداد پاراگرافهای متن را می شمارد (lname )
list word کلمات موجود در متن را به ترتیب از a تا z مرتب و چاپ می کند (lname )
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication16
{
class Program
{
class textmanager
{
public int wordcount(string wname)
{
int index, count = 0;
bool newword = true;
for (index = 0; index < wname.Length; index++)
switch (wname[index])
{
case ' ':
case ',':
case '.':
case ':':
case ';':
case '?':
newword = true;
break;
default:
if (((wname[index] >= 'a') && (wname[index] <= 'z'))
|| ((wname[index] >= 'A') && (wname[index] <= 'Z')))
if (newword == true)
{
newword = false;
count++;
}
break;
}
return count;
}
public int spacecount(string sname)
{
int index, count = 0;
for (index = 0; index < sname.Length; index++)
if (sname[index] == ' ')
count++;
return count;
}
public int paragraphcount(string lname)
{
int index, count = 0;
bool newline = true;
for (index = 0; index < lname.Length; index++)
if (lname[index] == '\n')
newline = true;
else
if (newline == true)
{
count++;
newline = false;
}
return count;
}
public void listword(string lname)
{
int index, count = 0, start = 0, i, j;
string[] words = new string[lname.Length + 1];
string temp;
bool newword = true;
for (index = 0; index < lname.Length; index++)
switch (lname[index])
{
case ' ':
case ',':
case '.':
case ':':
case ';':
case '?':
if (newword == false)
{
newword = true;
words[count - 1] = lname.Substring(start, index - start);
}
break;
default:
if (((lname[index] >= 'a') && (lname[index] <= 'z'))
|| ((lname[index] >= 'A') && (lname[index] <= 'Z')))
if (newword == true)
{
newword = false;
start = index;
count++;
}
break;
}
if (newword == false)
words[count - 1] = lname.Substring(start, index - start);
for (i = 2; i < count; i++)
for (j = 0; j < count - i; j++)
if (words[j].CompareTo(words[j + 1]) > 0)
{
temp = words[j];
words[j] = words[j + 1];
words[j + 1] = temp;
}
for (i = 0; i < count; i++)
Console.WriteLine(words[i]);
}
}
static void Main(string[] args)
{
textmanager t = new textmanager();
Console.WriteLine(t.wordcount("This is a test"));
Console.WriteLine(t.spacecount("This is a test"));
Console.WriteLine(t.paragraphcount("Paragraph #1\nParagraph #2"));
t.listword("This is a test");
Console.ReadKey(true);
}
}
}
using system;
using system.collections.generic;
using system.text;
namespace windowsapplication8
{
class textprocessor
{
string text;
public void wordcounter()
{
int counter = 0;
for (int i = 0; i < text.length; i++)
{
if (text == ' ')
counter = counter + 1;
}
}
public void paragrafcounter()
{
int conter1 = 0;
for (int i = 0; i < text.length; i++)
{
if (text == '\n')
conter1 = conter1 + 1;
}
}
static void main(string[] args)
{
console.writeline(("this is a test"));
console.writeline(("this is a test"));
console.readkey(true);
}
}
}
میشه همین رو بدون چیزهای سخت مثل اینا بنویسی
switch (wname[index])
{
case ' ':
case ',':
case '.':
case ':':
case ';':
case '?':
newword = true;
break;
default:
if (((wname[index] >= 'a') && (wname[index] <= 'z'))
|| ((wname[index] >= 'A') && (wname[index] <= 'Z')))
if (newword == true)
{
switch (wname[index])
{
case ' ':
case ',':
case '.':
case ':':
case ';':
case '?':
if (" ,.:;?".IndexOf(wname[index]) >= 0)
if (((wname[index] >= 'a') && (wname[index] <= 'z'))
|| ((wname[index] >= 'A') && (wname[index] <= 'Z')))
if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".IndexOf(wname[index]) >= 0)
public int wordcount(string wname)
{
int index, count = 0;
bool newword = true;
for (index = 0; index < wname.Length; index++)
if (" ,.:;?".IndexOf(wname[index]) >= 0)
newword = true;
else
if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".IndexOf(wname[index]) >= 0)
if (newword == true)
{
newword = false;
count++;
}
return count;
}