字符数组C ++元音

我试图制作一个使用switch语句的程序,看看char数组的元素是否是元音以及哪个是元音,但是我被困在如何检查元素的过程中:

int prob2() {
char uName[25] = "";
int voCo = 0;
cout<<"Enter you first and last name,under 25 chars please: ";
cin>>uName;
int i = 0;
while(i <= 25){
switch(i){
    case 1:

    voCo++;
    break;
    case 2:

    voCo++;
    break;
    case 3:

    voCo++;
    break;
    case 4:

    voCo++;
    break;
    case 5:

    voCo++;
    break;
    default:

    break;
}
i++;
}
cout<<"Your first and last name have: "<<voCo<<" vowels in them."<<endl;
return 0;
}
sienchou 回答:字符数组C ++元音

您可以尝试如下操作:

const std::string vowels = "aeiou";

const std::string name = "martin luther king,jr.";
const unsigned int name_length = name.length();
unsigned int vowel_count = 0U;
for (unsigned int i = 0U; i < name_length; ++i)
{
  if (vowels.find(name[i]) != std::string::npos)
  {
    ++vowel_count;
  }
}

不需要switch语句。这是许多可能的算法或实现之一。

编辑1:计数数组
您还可以使用一系列计数:

unsigned int counts[26] = {0};
for (unsigned int i = 0U; i < name_length; ++i)
{
    const c = std::tolower(name[i]);
    if (isalpha(c))
    {
        counts[c - 'a']++;
    }
}
const unsigned int vowel count =
    counts['a'] + counts['e'] + counts['i']
  + counts['o'] + counts['u'];
,

似乎您的意思是以下

#include <iostream>
#include <cctype>

using namespace std;

//...

size_t prob2() 
{
    const size_t N = 25;
    char uName[N] = "";

    size_t voCo = 0;

    cout<<"Enter you first and last name,under " << N << " chars please: ";
    cin.getline( uName,N );

    for ( char *p = uName; *p != '\0'; ++p ) *p = toupper( ( unsigned char )*p );

    for ( const char *p = uName; *p != '\0'; ++p )
    {
        switch( *p )
        {
        case 'A':
            voCo++;
            break;
        case 'E':
            voCo++;
            break;
        case 'I':
            voCo++;
            break;
        case 'O':
            voCo++;
            break;
        case 'U':
            voCo++;
            break;
        default:
            break;
        }
    }

    cout<<"Your first and last name have: "<<voCo<<" vowels in them."<<endl;

    return voCo;
}
,

首先,使用户交互与解决您的需求的逻辑脱钩。我认为我们可以放心地假设您可以在这种情况下收集输入并将其保存为字符串。因此,我们不会为此浪费时间。

我们将专注于开发和测试可满足要求的代码。在标准C ++中。现在这是游泳池的尽头。代码。

// mike.h
#pragma once

// std::string view requires C++17
#include <string_view>

// always use namespace,to avoid name clashes
namespace mike {

    // make 'sv' the string_view literal available
    using namespace std::string_view_literals;

    // declare and define compile time
    // string view literal
    // 'constexpr' guarantees compile time
    // notice the use of 'sv'
    constexpr auto vowels = "eaiouEAIOU"sv;

    // compile time function to count literals 
    // again 'constexpr' guarantees compile time
    // inline gurantees we can include this header many times
    // without making accidental duplicates of `count_vowels`
    // 'in_' argument has 'std::string_view' passed by value
    // pass by value is preferred standard C++ method
    // of functions arguments passing
    //  'std::string_view' is standard C++ preferred type
    // to pass strings into functions
    inline constexpr size_t
        count_vowels(std::string_view in_) 
    {
        // return type is size_t
        // we can count very large number of vowels
        // but all at compile time
        size_t rezult{}; 
        // this is C+17 'range for'
        // we cast implicitly references to input elements
        // from,`char const &` to `int const &`
        // cost of that is very likely 0
        for (int const & ch_ : in_)
            for (int const & v_ : vowels)
                // there is no if() here
                // we simply add 0's or 1's,to the rezult
                // false is 0,true is 1
                // the correct by the book way of coding that is
                // static cast from bool to int
                // rezult +=  static_cast<int>( v_ == ch_ ) ;
                rezult += v_ == ch_  ;
        return rezult;
    }

    // runtime speed of this call is 0 (zero)
    // all happens at compile time
    // notice how we pass normal string literal
    // no need to create string_view
    constexpr size_t r1 
        = count_vowels("abra ca dabra");

    // no runtime tests necessary
    // `static_assert()` is compile time assert
    // failure message is optional
    static_assert(r1 == 5,"compile time calculation failed,'abra ca dabra',must contain 5 vowels");
} // mike ns

希望有很多评论。解决方案不使用switch()语句或if()语句。多亏了标准的C ++构造,当使用现代优化编译器进行编译时,代码非常简单,富有弹性,并且可能非常快。

解决方案也在编译时起作用。这并不能阻止您在运行时方案中使用它。虽然,我会再次建议使用本机char数组。 std::string在这里可能是完美的选择。

std::string input_ = collect_user_input() ;
int rezult = count_vowels(input_);

享受标准的C ++ ...

本文链接:https://www.f2er.com/3100311.html

大家都在问