【问题标题】:Lowercase letters that did not appear in the array, in order [closed]未出现在数组中的小写字母,按顺序[关闭]
【发布时间】:2019-11-30 12:23:53
【问题描述】:

定义一个函数,接收一个字符数组(及其大小),然后按顺序打印出所有未出现在数组中的小写字母。

例如:

输入:Article 1: All human beings are born free and equal in dignity and rights.
输出:jkpvwxz

到目前为止,这是我的代码:

#include <iostream>
using namespace std;

void missing(char string[], int size) {
    for (int i = 0; i < size; i ++) {
    }
}

int main(){
}

问题

待定

【问题讨论】:

  • 您好像忘记为 your 任务编写代码了。
  • 是这样的,我们不应该解决你得到的任务,你需要表现出自己尝试和解决问题的努力,如果你在某个点上遇到困难,那么社区就会提供帮助;请阅读stackoverflow.com/help/how-to-ask
  • 如果你还没有决定一个问题,就没有必要发帖了吗?

标签: c++ loops for-loop while-loop c-strings


【解决方案1】:

我们初学者应该互相帮助。

如果函数处理字符串,那么函数的第二个参数是多余的。

不使用标准容器的简单方法(例如std::set)可以如下所示

#include <iostream>

void missing( const char s[] ) 
{
    const char *letter = "abcdefghijklmnopqrstuvwxyz";

    for ( size_t i = 0; letter[i] != '\0'; i++ )
    {
        size_t j = 0;

        while ( s[j] != '\0' and s[j] != letter[i] ) j++;

        if ( s[j] == '\0' ) std::cout << letter[i];
    }        
}

int main()
{
    while ( true )
    {        
        const size_t N = 100;
        char s[N];

        std::cout << "Enter a sentence: ";

        if ( not std::cin.getline( s, N ) or s[0] == '\0' ) break;

        missing( s );

        std::cout << '\n';
    }        
}

程序输出可能看起来像

Enter a sentence: Article 1: All human beings are born free and equal in dignity and rights
jkpvwxz
Enter a sentence:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-15
    • 2017-11-21
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 2013-07-14
    相关资源
    最近更新 更多