You'll need to do some string manipulation. What you actually need here is to look up String.Split and String.Substring and String.LastIndexOf - Just naming a few that immediately pops into my head.
You probably also want to use string.Split(string, StringSplitOptions) giving StringSplitOptions.RemoveEmptyEntries as the second argument.
You might also think about canonicalizing your string to contain only a restricted character set (namely "A-Za-z0-9. "). That should avoid any problems if non-space whitespace characters are present.
Best Regards,
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Hey,
I'm just learning to program, so I've tried to add some of the cool stuff that I've learned into this (LINQ, Yield, etc)
class Program
{
static void Main(string[] args)
{
const string paragraph = "Sentence one. Sentence two contains some more words. Sentence three; thinking.";
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestApplications
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 1 && args[0] == "/?")
{
Console.WriteLine("SYNTAX: TestApplications <Sentence>");
#if DEBUG
Pause();
#endif
return;
}
if (args.Length < 1)
{
Console.WriteLine("Invalid Syntax! /? for help.");
#if DEBUG
Pause();
#endif
return;
}
string sentence = args[0];
if (String.IsNullOrEmpty(sentence))
{
Console.WriteLine("String is empty!");
return;
}
foreach (List<int> count in ParseSentence(sentence))
{
Console.WriteLine(String.Format("Sentence {0} = {1} words.", count[0], count[1]));
}
#if DEBUG
Pause();
#endif
}
private static IEnumerable<List<int>> ParseSentence(string input)
{
int wordCount = 0;
int sentenceCount = 0;
List<int> ret = new List<int>();
for (int i = 0; i < input.Length; i++)
{
if (input[i] == ' ') //End of word
{
++wordCount;
continue;
}
if (input[i] == '.') //End of sentence
{
++wordCount; //Period should be at the end of a word...
++sentenceCount;
ret.Add(sentenceCount);
ret.Add(wordCount);
yield return ret;
ret = new List<int>();
wordCount = 0;
}
}
}
private static void Pause()
{
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
}
}
}
Last edited by Deranged; June 7th, 2012 at 06:44 PM.
Bookmarks