soln3_6.cpp

来自「Visual C++ 2005的源代码」· C++ 代码 · 共 44 行

CPP
44
字号
// Soln3_6.cpp : main project file.
// Analyzing a string using a for each loop

#include "stdafx.h"

using namespace System;


int main(array<System::String ^> ^args)
{
  int vowels = 0;
  int consonants = 0;
  int nonAlpha = 0;
  int totalChars = 0;
  String^ string = L"\"Hello!\" he shouted, \"Are we going now?\".";
  for each(wchar_t ch in string)
  {
    ++totalChars;
    if(Char::IsLetter(ch))
    {
      ch = Char::ToLower(ch);      // Convert to lowercase
      switch(ch)
      {
      case 'a': case 'e': case 'i':
      case 'o': case 'u':
        ++vowels;
        break;
      default:
        ++consonants;
        break;
      }
    }
    else
      ++nonAlpha;
  }
  Console::WriteLine(string);
  Console::WriteLine(L"The string contains {0} vowels, {1} consonants,",
                                                              vowels, consonants);
  Console::WriteLine(L"{0} non-alphabetic characters, which is {1} in total.",
                                                              nonAlpha, totalChars);

    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?