打印由用户定义的数组,第一个数字确定单词的大小,第二个数字确定行的大小
Printing an array defined by the user, first number determines the size of a word, second determines size of the row
我正在尝试创建一个循环,该循环打印一个充满字母的数组,并以用户确定的特定格式打印它们。例如,格式由用户在命令行中输入(不是它也删除标点符号):
程序.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;
}
相关文章:
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 等待整个 omp 块完成,然后再调用第二个函数
- 我想在C++中读取一些多个字符,但它永远不会读取第二个字符
- 如何在创建自定义迭代器时获得 std::p air 的第一个和第二个?
- WinAPI 在单击第一个对话框上的按钮控件并销毁第一个对话框后创建第二个对话框
- 将第二个 GATT 服务添加到 Movesense 容器
- 如何在 c++ 中根据第二个元素按降序对列表进行排序
- 对的排序向量 (std::vector<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- 比较 2 个向量并从第二个向量中删除在第一个 - c++ 中找不到的元素
- 为什么这个程序没有打印返回的迭代器的正确第二个元素?
- 使第二个类的构造函数成为第一个类中的友元函数
- 为什么第二个代码给出了预期的结果,而第一个代码却没有?
- 为什么第一个代码块产生垃圾值,而第二个代码块将类成员的值相加?
- 打印无序映射的第二个元素,即集合
- 为什么断点显示数组的第二个值是一个大数字?额外学分工作
- 为什么第二个数字总是 0
- 如何在C 循环中找到第二个最大数字
- 第一个和第二个数字之间能被 4 和 6 整除的所有整数的总和
- 打印由用户定义的数组,第一个数字确定单词的大小,第二个数字确定行的大小
- 为什么 for 循环没有达到循环的第二个条件中设置的数字