我正在尝试创建一个打印填充了字母的数组的循环,并以用户确定的特定格式打印它们。例如,用户在命令行中输入格式(不是它也会删除标点符号):
program.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;
}
答案 0 :(得分: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;
}