简体   繁体   中英

How do I print out a 2-D array of the alphabet using a keyword?

So I was given the task to print out a 2-D array which is 5x5 having each letter of the alphabet in each of the locations of the array. I am supposed to use a keyword and print that in the rows of the 2-D array, then print out the rest of the letters of the alphabet. The letters should only be printed once in the whole array and 'Z' is excluded.

Not to good but this is what I have so far:

#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

bool correct_keyword(char);

int main()
{
    ifstream fin ("infile.txt");
    char array[5][5];
    char i, keyword;
    bool correct;

    cout << "Type in the key word: " << endl;
    cin >> keyword;
    correct_keyword(keyword);

        for (int row = 0; row < 5; row++)
        {
            for (int col = 0; col < 5; col++)
            {
                keyword= array[row][col];
            }
        }
        for (int row = 0; row < 5; row++)
        {
            for (int col = 0; col < 5; col++)
            {
                cout << keyword << " ";
            }
            cout << endl;
        }

        return 0;
}

bool correct_keyword(char keyword)
{
    bool correct = false;
    if (keyword != correct)
        return true;
    else
        return 1;
}

If my keyword was "Fish" it would look something like this:

    0   1   2   3   4
0   F   I   S   H   A
1   B   C   D   E   G
2   J   K   L   M   N
3   O   P   Q   R   T
4   U   V   W   X   Y

Any help would be appreciated!! Thank you very much!

If you have already declared the alphabet in the 2D array (5x5), let's say in char alphabet[5][5] , you can print these values using nested for loops:

for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++)
        std::cout << alphabet[i][j] << std::endl;

The code will print all letters in new lines, if you need to print them as a 5x5 array, then this should work:

for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 5; j++)
    {
        std::cout << alphabet[i][j] << " "; // separate letters with a space
    }
    std::cout << std::endl;
}

Why not use std::string to store you keyword? That provides .find() to check whether a character is included. You don't have to worry about a 2D array, all you need is your std::string and a counter and you can output the '\\n' after every 5 th character providing your output in a 5x5 grid.

How do you handle keywords with duplicate characters? Are they just considered invalid keywords? What about "wizard" would it likewise be an invalid keyword because it contains the excluded 'z' character? (I guess both would have to be, otherwise you would not be able to fill though 'y' in your 5x5 grid)

With that said, you could simply take your keyword as the first argument to your program and keep a counter of the number of characters output and do something simple like:

#include <iostream>
#include <string>

#define WIDTH 5

int main (int argc, char **argv) {

    std::string s = argc > 1 ? argv[1] : "help";
    size_t n = 0;

Now you need to check whether your keyword is valid, no duplicate characters and no 'z' , eg

    for (size_t i = 0; i < s.size() - 1; i++)   /* check for duplicate chars */
        if (s.find(s.at(i), i+1) != std::string::npos) {
            std::cerr << "error: '" << s.at(i) << "' duplcate char.\n";
            return 1;
        }
    if (s.find('z') != std::string::npos) {     /* check for 'z' in input */
        std::cerr << "error: input contains 'z'.\n";
        return 1;
    }

All that is left is outputting your keyword, followed by the remaining characters in the alphabet (minus 'z' ). You can do that with two loops, eg

    for (; n < s.size(); n++) {     /* output keyword, keep n as count */
        if (n && n % WIDTH == 0)    /* impose WIDTH no. chars per-line */
            std::cout << '\n';
        std::cout << " " << s.at(n);
    }


    for (int i = 'a'; i < 'z'; i++) {   /* output alphabet, skipping chars */
        if (s.find((char)i) == std::string::npos) {
            std::cout << " " << (char)i;
            n++;
            if (n && n % WIDTH == 0)
                std::cout << '\n';
        }
    }

( note: the above uses lowercase characters, but you can simply change to uppercase if that is a requirement by swapping characters)

Putting it altogether, you could do:

#include <iostream>
#include <string>

#define WIDTH 5

int main (int argc, char **argv) {

    std::string s = argc > 1 ? argv[1] : "help";
    size_t n = 0;

    for (size_t i = 0; i < s.size() - 1; i++)   /* check for duplicate chars */
        if (s.find(s.at(i), i+1) != std::string::npos) {
            std::cerr << "error: '" << s.at(i) << "' duplcate char.\n";
            return 1;
        }
    if (s.find('z') != std::string::npos) {     /* check for 'z' in input */
        std::cerr << "error: input contains 'z'.\n";
        return 1;
    }

    for (; n < s.size(); n++) {     /* output keyword, keep n as count */
        if (n && n % WIDTH == 0)    /* impose WIDTH no. chars per-line */
            std::cout << '\n';
        std::cout << " " << s.at(n);
    }


    for (int i = 'a'; i < 'z'; i++) {   /* output alphabet, skipping chars */
        if (s.find((char)i) == std::string::npos) {
            std::cout << " " << (char)i;
            n++;
            if (n && n % WIDTH == 0)
                std::cout << '\n';
        }
    }
}

( note: adding any surrounding Row or Column Headings, eg "0 1 2 3 4" is left to you)

Example Use/Output

With default "help" as keyword:

$ ./bin/alphabet
 h e l p a
 b c d f g
 i j k m n
 o q r s t
 u v w x y

With "absent" as keyword:

$ ./bin/alphabet absent
 a b s e n
 t c d f g
 h i j k l
 m o p q r
 u v w x y

Or an invalid keyword:

$ ./bin/alphabet hello
error: 'l' duplcate char.

You are free to use a 2D array, but it just seems like that would complicate input and storage of the keyword. Look things over and let me know if something like this would work and whether you have any further questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM