I have been set a task of creating a c# console text analysis program. The program allows the user to enter a sentence word by word a single full stop is the end of the sentence a double full stop is to break the loop and give the analysis of the text
I have the program counting words and sentences correctly.
My question is: How can I change my code so that the program will not count full stops as a character?
Below is my code thus far
case "1":
string UserSentence="";
string newString="";
string UserWord;
int SentenceCount=1;
int WordCount=0;
double CharCount=0;
Console.WriteLine("You have chosen to type in your sentance(s) for analysis.\nPlease input each word then press enter.\n\nUse one full stop to end the sentence.\nUse two full stops to finish inputting sentences");
while (true)
{
UserWord = Console.ReadLine();
WordCount++;
UserSentence = UserSentence+UserWord;
if (UserWord == "..")
{
CharCount=CharCount-2;
WordCount--;
break;
}
if (UserWord == ".")
{
CharCount=CharCount-1;
WordCount--;
SentenceCount++;
}
}
foreach (char c in UserSentence)
{
if (c ==' ')
continue;
newString += c;
}
CharCount = newString.Length;
Console.WriteLine("Their are {0} characters",CharCount);
Console.WriteLine("Their are {0} Sentences",SentenceCount);
Console.WriteLine("Their are {0} Words",WordCount);
break;
I have tried to correct the character count by subtracting 2 or 1 depending on the amount of full stops however it does not work
Thanks for any help in advance.
Here you are simply overwriting the value of
CharCount, discarding all the subtraction you did earlier:It can be changed to:
In order to give the correct result.
There are other options, such as counting the number of
.in the sentence, replacing all the.in a sentence in an empty string before getting its length and many more.Note on style: in C#, local variables are normally camelCase, not PascalCase.