打印由用户定义的数组,第一个数字确定单词的大小,第二个数字确定行的大小

Printing an array defined by the user, first number determines the size of a word, second determines size of the row

本文关键字:数字 第二个 单词 第一个 用户 定义 数组 打印      更新时间:2023-10-16

我正在尝试创建一个循环,该循环打印一个充满字母的数组,并以用户确定的特定格式打印它们。例如,格式由用户在命令行中输入(不是它也删除标点符号):

程序.exe 5 8 "我正在尝试创建一个循环,该循环打印一个充满字母的数组,并以特定的格式打印它们"

imtry ingto creat ealoo pthat print sanar rayfi
lledw ithle tters andpr intst hemin aspec ific

我不知道我该如何创建这个循环。数组是 1 维的。

编辑:这里似乎有些混乱,我已经有了所有代码,我只是不知道如何在满足第一个数字后实现空格并在满足第二个数字后创建新行。我想做的是在放置每个字符后检查放置了多少个字符,然后在适当的时候放入空格中,然后向前递增另一个数字。到达第二个数字后,创建一个新行并将该数字设置为 0。

这是我到目前为止拥有的一切:

#include <iostream>
#include <string>
#include <cstddef>
#include <stdio.h>
#include <ctype.h>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
    int wordSize = atoi(argv[1]) ;
    int rowSize = atoi(argv[2]) ;
    string test = argv[3] ;
    //Make vowels uppercase
    size_t found = test.find_first_of("aeiou");
    while (found!=string::npos)
    {
        if (islower(test[found])) ;
        {
            test[found] = toupper(test[found]) ;
            found=test.find_first_of("aeiou",found+1) ;
        }
    }
    //Make consonants lowercase
    size_t foundLower = test.find_first_of("BCDFGHJKLMNPQRSTVWXYZ") ;
    while (foundLower!=string::npos)
    {
        if (islower(test[foundLower])) ;
        {
            test[foundLower] = tolower(test[foundLower]) ;
            foundLower=test.find_first_of("BCDFGHJKLMNPQRSTVWXYZ",foundLower+1) ;
        }
    }
    //remove punctuation
    for (int i = 0, len = test.size(); i < len; i++)
    {
        if (ispunct(test[i]))
        {
            test.erase(i--, 1) ;
            len = test.size() ;
        }
    }
    //print the results in the blocks defined by the user
    //int size = (sizeof(test) / sizeof(test[0])) ;
    for (int i = 0; i < wordSize; i++ )
    {
        //if (i = i / 5)
        //{
        //    cout << " " ;
        //}
        //else
        //{
        //    cout << test[i] ;
        //}
    }
    //while
    //cout << test << 'n' ;
    return 0;
}

你去吧。我对你的一些代码做了一些小的修复,并留下了评论,解释了我修复了什么以及为什么(我现在只记得if语句后面的几个分号 - 你不希望那些在那里!

循环是你问题的答案。它使用 substr 或子字符串分解输入,并打印它。还有一些基本的健全性检查,以确保我们可以继续以完整的单词大小调用substr,并且之后仍然继续迭代。还有一个滚动计数器,可以计算我们打印了多少字,并在适当的时间点打印endl

还值得指出的是,按照现在的代码,命令窗口将在打印所有内容后立即关闭。您可能希望在末尾放置一个system("pause");getch();,这样它就不会立即关闭。

#include <iostream>
#include <string>
#include <cstddef>
#include <stdio.h>
#include <ctype.h>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[])
{
  int wordSize;
  int wordCounter = 0;
  int rowSize;
  string test;
  if (argc < 4) //make sure we got all the parameters we needed; the code will crash without them
  {
    cout << "Error: not enough parameters";
    return 1;
  }
  wordSize = atoi(argv[1]);
  rowSize = atoi(argv[2]);
  test = argv[3];
  //Make vowels uppercase
  size_t found = test.find_first_of("aeiou");
  while (found != string::npos)
  {
    if (islower(test[found])) //minor fix here, you don't want the semicolon!
    {
      test[found] = toupper(test[found]);
      found = test.find_first_of("aeiou", found + 1);
    }
  }
  //Make consonants lowercase
  size_t foundLower = test.find_first_of("BCDFGHJKLMNPQRSTVWXYZ");
  while (foundLower != string::npos)
  {
    if (islower(test[foundLower])) //another removed semicolon here
    {
      test[foundLower] = tolower(test[foundLower]);
      foundLower = test.find_first_of("BCDFGHJKLMNPQRSTVWXYZ", foundLower + 1);
    }
  }
  //remove punctuation
  for (int i = 0, len = test.size(); i < len; i++)
  {
    if (!isalnum(test[i])) //we can't test for punctuation with ispunct, because spaces get through
    {
      test.erase(i--, 1);
      len = test.size();
    }
  }
  while (!test.empty())
  {
    if (test.length() < wordSize) //make sure we don't go out-of-bounds
    {
      wordSize = test.length();
    }
    cout << test.substr(0, wordSize);
    cout << " ";
    if (test.length() >= wordSize) //again, make sure we don't go out-of-bounds
    {
      test = test.substr(wordSize);
    }
    else
    {
      test = "";
    }
    wordCounter++;
    if (wordCounter == rowSize)
    {
      cout << std::endl;
      wordCounter = 0;
    }
  }
  return 0;
}
相关文章: