Hey guys. I'm using CLR to try and count the number of lowercase, symbols, capital letters, consonants and vowels in a string. I think I've got everything else figured out, but I can't seem to get my program to count capital letters correctly. Here's the code. Thanks in advance.
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
int vowels(0);
int consonants(0);
int nonAlpha(0); // Define 4 variables as int
int totalChars(0);
int uppercase(0);
String^ proverb(L"I'm a |itt|e snowman short and stout, here is my h@ndle, here is my spout ");
for each(wchar_t ch in proverb)
{
++totalChars; //for each iteration of the loop, this counts ie this = loop counter
if(Char::IsLetter(ch)) //start of loop tests to see if characters in string are letters, else = nonalpha
{ I think my problem starts here, but I can't figure out quite how to do it
if(ch = Char::ToLower(ch), ++uppercase)
//convert to lowercase and count uppercase?
switch(ch) //switch to check for vowels
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
++vowels; //counts iterations of the loop for vowels
break;
Console::WriteLine(L"The string is made up of {0} vowels, {1} consonants, {2} symbols.", vowels, consonants, nonAlpha);
Console::WriteLine(L"The string also contains {0} characters.", totalChars);
Console::WriteLine(L"Additionally there are {0} capital letters.", uppercase);
Console::ReadLine();
= is assignment, == is check for equality. We all got hit by that at some point. But good job spotting the source on your own. (the ++uppercase should be inside the if block though, not in the condition. I am not sure whether the , operator evaluates to the left or the right side, but I am quite certain that ++uppercase will always be executed no matter what the condition before evaluates to. Besides, it looks ugly.
Figured it out. I couldn't get ToLower or ToUpper to work the way I wanted so I went with another if statement based on the numeric value of capital letters.
Revised code looks like this. Tested it, seems to work!
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
int vowels(0);
int consonants(0);
int nonAlpha(0); // Define 3 variables as int
int totalChars(0);
int uppercase(0);
String^ proverb(L"DiE IN @ fire Stupid!QZXN");
for each(wchar_t ch in proverb)
{
++totalChars; //for each iteration of the loop, this counts ie this = loop counter
if(Char::IsLetter(ch)) //start of loop, tests to see if characters in string are letters, else = nonalpha
{
if(ch <= 'Z')
{
++uppercase; //Test for numerical value less than or = to 'Z'
}
switch(ch) //switch to check for vowels
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
++vowels; //counts iterations of the loop for vowels
break;
Console::WriteLine(L"The string is made up of {0} vowels, {1} consonants, {2} symbols.", vowels, consonants, nonAlpha);
Console::WriteLine(L"The string also contains {0} characters (including spaces).", totalChars);
Console::WriteLine(L"Additionally there are {0} capital letters.", uppercase);
Console::ReadLine();