p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 (http://p2p.wrox.com/book-ivor-hortons-beginning-visual-c-2008-isbn-978-0-470-22590-5-394/)
-   -   Chapter 3 exercise 6 (http://p2p.wrox.com/book-ivor-hortons-beginning-visual-c-2008-isbn-978-0-470-22590-5/86602-chapter-3-exercise-6-a.html)

 earlmw February 8th, 2012 11:11 PM

Chapter 3 exercise 6

In the book for chapter 3 exercises the author asks:
"Write a CLR console program that defines a string (as type String^) and then analyzes the characters in the string to discover the number of uppercase letters, the number of lowercase letters, the number of non-alphabetic characters, and the total number of characters in the string."
I noticed that the downloaded code solution is finding "vowels and consonants"
I have written code to solve the exercise in the book.
code
// Solu3_6.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
int caps = 0;
int small = 0;
int nalpha = 0;
int totalchar = 0;

String^ string = L"In 1978, Lasse Nielsen\'s film \"Du er ikke alene\" was released.";

for each(wchar_t ch in string)
{
++totalchar;
if(Char::IsLetter(ch))
{
if(Char::IsUpper(ch))
++caps;
else
++small;

}

if(Char::IsDigit(ch))
++nalpha;
if(Char::IsPunctuation(ch))
++nalpha;

}
Console::WriteLine(string);

Console::WriteLine(L"\nThe total of capitals is {0} and small letters is {1}.", caps, small);

Console::WriteLine(L"The total non-alphabetic is {0} and total characters is {1}.",nalpha, totalchar);
return 0;
}
/code

 earlmw February 10th, 2012 08:10 PM

Code for Chapter 3 Exercise problem #6

Code:

```// Solu3_6.cpp : main project file. #include "stdafx.h" using namespace System; int main(array<System::String ^> ^args) {         int caps = 0;         int small = 0;         int nalpha = 0;         int totalchar = 0;         String^ string = L"In 1978, Lasse Nielsen\'s film \"Du er ikke alene\" was released.";         for each(wchar_t ch in string)         {                 ++totalchar;                 if(Char::IsLetter(ch))                 {                         if(Char::IsUpper(ch))                                 ++caps;                         else                                 ++small;                 }                 if(Char::IsDigit(ch))                         ++nalpha;                 if(Char::IsPunctuation(ch))                         ++nalpha;         }         Console::WriteLine(string);         Console::WriteLine(L"\nThe total of capitals is {0} and small letters is {1}.", caps, small);         Console::WriteLine(L"The total non-alphabetic is {0} and total characters is {1}.",nalpha, totalchar);         return 0; }```

 All times are GMT -4. The time now is 02:34 PM.