C + +/CLI programming-using the For Each loop to access characters in a string

Source: Internet
Author: User

In addition to the 3 circular statements defined in ISO/IEC C + + ,C + +/CLI also provides a for each Statement. In C + +/CLI , The For Each loop is more efficient than several other forms of cycle efficiency.


#include "stdafx.h" using namespace System;int Main (array <system::string ^> ^args) {int vowels (0), consonants (0); string^ Proverb (L "A nod is as good as a wink to a blind horse."); For each (wchar_t ch in proverb) {if (Char::isletter (ch)) {ch = char::tolower (CH), switch (CH) {case ' a ': Case ' e ': Case ' I ': CAs E ' o ': Case ' u ': ++vowels;break;default:++consonants;break;}}} Console::WriteLine (proverb); Console::WriteLine (L "The proverb contains {0} vowels and {1} consonants.", vowels, consonants); Console::readline (); return 0;}

The program calculates the number of vowels and consonants in the string referenced by the variable proverb by repeating each character in the string with a For Each loop.


C + +/CLI programming-using the For Each loop to access characters in a string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.