[英]Stuck on removing whitespace from string without using any helper code c++
Create a program titled str_compress.cpp.创建一个名为 str_compress.cpp 的程序。 This program will take a sentence input and remove all spaces from the sentence.该程序将接受一个句子输入并从句子中删除所有空格。 (A good first step in encryption programs) Make sure that both the input and output strings are all stored in a single variable each. (加密程序的第一步)确保输入和 output 字符串都存储在一个变量中。 Do not use numbers or symbols.不要使用数字或符号。 Include both upper-case and lower-case letters.包括大写和小写字母。 Account for cases with multiple spaces anywhere.考虑任何地方有多个空格的案例。
This is what I have so far:这是我到目前为止所拥有的:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i = 0, j = 0, len;
string str;
cout << "Enter string: ";
getline(cin, str);
len = str.length();
for (i = 0; i < len; i++)
{
if (str[i] == ' ')
{
for (j = i; j < len; j++)
{
str[j] = str[j + 1];
}
len--;
}
}
cout << str << endl;
system("pause");
return 0;
}
I can eliminate spaces, but only one at a time.我可以消除空格,但一次只能消除一个。 If I copy and paste the for
loop, I can remove all spaces for how many loops there are.如果我复制并粘贴for
循环,我可以删除有多少个循环的所有空格。 I'm thinking that I can loop the for
loop over and over until all spaces are gone, but I'm not sure how to do that.我在想我可以一遍又一遍地循环for
循环,直到所有空格都消失,但我不知道该怎么做。 Also, I can't use anything like remove_all()
or erase()
.另外,我不能使用remove_all()
或erase()
之类的东西。
This is a strong clue for how the authors of your exercise want you to write your code:这是您练习的作者希望您如何编写代码的有力线索:
Make sure that both the input and output strings are all stored in a single variable each确保输入和 output 字符串都存储在一个变量中
You should make a new string:您应该创建一个新字符串:
string new_str;
Use your loop over the input string.在输入字符串上使用循环。 For each char
in the string, check whether it is a space.对于字符串中的每个char
,检查它是否是一个空格。 If yes, do nothing.如果是,什么也不做。 If no, append it to the output string:如果没有,则将 append 转至 output 字符串:
for (i = ...)
{
char c = str[i];
if (c != ' ')
new_str.push_back(c);
}
Your loop's logic when removing a space is wrong.删除空格时循环的逻辑是错误的。 For instance, after removing a space, you then skip the next char
in the string, which may be another space.例如,删除一个空格后,您会跳过字符串中的下一个char
,它可能是另一个空格。 Also, although you are decrementing the len
, you don't resize the string to the new len
before printing the new str
value.此外,尽管您正在减少len
,但在打印新的str
值之前,您不会将字符串的大小调整为新的len
。
It should look more like this:它应该看起来更像这样:
#include <iostream>
#include <string>
using namespace std;
int main()
{
size_t i, j, len;
string str;
cout << "Enter string: ";
getline(cin, str);
len = str.length();
i = 0;
while (i < len)
{
if (str[i] == ' ')
{
for (j = i + 1; j < len; ++j)
{
str[j - 1] = str[j];
}
--len;
}
else
++i;
}
str.resize(len);
cout << str << endl;
/* or, if you are not allowed to use resize():
cout.write(str.c_str(), len);
cout << endl;
*/
/* or, if you are not allowed to use write():
if (len < str.length())
str[len] = '\0';
cout << str.c_str() << endl;
*/
system("pause");
return 0;
}
However, your instructions do say to " Make sure that both the input and output strings are all stored in a single variable each ", which implies that separate std::string
variables should be used for input and output, eg:但是,您的指令确实说“确保输入和 output 字符串都存储在一个变量中”,这意味着应将单独的std::string
变量用于输入和 output,例如:
#include <iostream>
#include <string>
using namespace std;
int main()
{
size_t i, j, len;
string str, str2;
cout << "Enter string: ";
getline(cin, str);
str2 = str;
len = str2.length();
i = 0;
while (i < len)
{
if (str2[i] == ' ')
{
for (j = i + 1; j < len; ++j)
{
str2[j - 1] = str2[j];
}
--len;
}
else
++i;
}
str2.resize(len);
cout << str2 << endl;
/* or:
cout.write(str2.c_str(), len);
cout << endl;
*/
/* or:
if (len < str2.length())
str2[len] = '\0';
cout << str2.c_str() << endl;
*/
system("pause");
return 0;
}
Alternatively:或者:
#include <iostream>
#include <string>
using namespace std;
int main()
{
size_t i, j, len;
string str, str2;
cout << "Enter string: ";
getline(cin, str);
len = str.length();
str2.reserve(len);
for(i = 0; i < len; ++i)
{
char ch = str[i];
if (ch != ' ')
str2 += ch;
}
cout << str2 << endl;
system("pause");
return 0;
}
This is what worked for me.这对我有用。 Thank you everyone for the help!!谢谢大家的帮助!!
int main()
{
int i, j, len;
string str, str2;
cout << "Enter string: ";
getline(cin, str);
len = str.length();
for (i = 0; i < len; ++i)
{
char ch = str[i];
if (ch != ' ')
str2 += ch;
}
cout << str2 << endl;
system("pause");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.