如何将点字符添加到字符串中的字符
How to add dot character to a character in string?
我想在字符串中的另一个字符旁边添加'.'字符,但我不知道该怎么做? 可能吗?
#include <iostream>
#include <string.h>
using namespace std;
int main(int argc, char *argv[]) {
string input;
char dot='.';
cin>>input;
for(int i=0;i<input.length();i++)
{
if( input[i]>=65 && input[i]<=90)
{
input[i]=input[i]+32;
}
if( (input[i]=='a') || (input[i]=='e') || (input[i]=='i') || (input[i]=='o') || input[i]=='y' || input[i]=='u' )
{
input.erase(i,i+1);
}
input[i]+=dot;
}
cout<<input<<endl;
return 0;
}
来自 cpluplus.com 引用 ( http://www.cplusplus.com/reference/string/string/insert/)
// inserting into a string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str="to be question";
string str2="the ";
string str3="or not to be";
string::iterator it;
// used in the same order as described above:
str.insert(6,str2); // to be (the )question
str.insert(6,str3,3,4); // to be (not )the question
str.insert(10,"that is cool",8); // to be not (that is )the question
str.insert(10,"to be "); // to be not (to be )that is the question
str.insert(15,1,':'); // to be not to be(:) that is the question
it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)
str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
cout << str << endl;
return 0;
}
另外,请检查以下链接:
http://www.cplusplus.com/reference/string/string/http://www.cplusplus.com/reference/string/string/append/http://www.cplusplus.com/reference/string/string/push_back/
在尝试编写代码之前,您应该编写详细的代码规范它应该做什么。 用你的代码,我只能猜测:转换为小写(天真地,假装你只会遇到 ASCII 中的 26 个无重音字母),然后删除所有元音(再次,非常天真,因为确定某物是否是元音是不平凡的,即使在英语 - 考虑 y 在 yet 和 day),最后在每个字符后插入一个点。 最明显的方法这样做是这样的:
std::string results;
for ( std::string::const_iterator current = input.begin(),
end = input.end();
current != end;
++ current ) {
static std::string const vowels( "aeiouAEIOU" );
if ( std::find( vowels.begin(), vowels.end(), *current )
!= vowels.end() ) {
results.push_back(
tolower( static_cast<unsigned char>( *current ) ) );
}
results.push_back( '.' );
}
但同样,我只是猜测你想做什么。
另一种选择std::transform
是在初始字符串使其全部小写。 如果你正在这样做定期的事情,你会有一个ToLower
的功能对象;否则,写起来可能太麻烦了一个只是为了能够使用一次std::transform
。
我假设你想要这个输入:
Hello world!
要为您提供此输出,请执行以下操作:
h.ll. w.rld!
与其尝试就地修改字符串,不如随时生成一个新字符串:
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
string input;
getline(cin, input);
string output;
const string vowels = "aeiouy";
for (int i = 0; i < input.size(); ++i) {
const char c = tolower(input[i]);
if (vowels.find(c) != string::npos) {
output += '.';
} else {
output += c;
}
}
cout << output << 'n';
return 0;
}
笔记:
<cctype>
是为了toupper()
.<string.h>
已弃用;请使用<string>
。用
getline()
阅读整行;istream::operator>>()
读字。使用
tolower()
、toupper()
等进行字符转换。c + 32
没有描述您的意图。当你需要比较时,
c >= 'A' && c <= 'Z'
会起作用;你不需要使用ASCII码。将
const
用于不会更改的内容。
我不确定这个老问题是如何被撞回当前列表的,但在查看答案后,如果输入超过一个单词,看起来所有人都会错过分数。从您的评论中,您似乎想要删除所有元音,并在删除发生位置之前的字符之前放置一个'.'
。因此,您的示例"tour"
变得".t.r"
。
其他答案中汲取经验,并无耻地从元音列表中删除'y'
,您可以执行以下操作:
#include <iostream>
#include <string>
int main()
{
std::string input;
if (!getline (std::cin, input)) {
return 1;
}
size_t i = 0;
for (; input[i]; i++)
{
switch (input[i])
{
case 'A': /* case fall-through intentional */
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
{
size_t pos = input.find_first_not_of("AEIOUaeiou", i+1);
if (pos == std::string::npos) {
pos = input.length();
}
input.erase(i, pos-i);
if (pos - i > 1) {
input.insert(i, 1, '.');
}
input.insert(i-1, 1, '.');
break;
}
}
}
std::cout << input << 'n';
}
示例使用/输出
您的示例:
$ ./bin/vowels-rm-mark
tour
.t.r
一个更长的例子:
$ ./bin/vowels-rm-mark
My dog has fleas and my cat has none.
My .dg .hs f.l.s. nd my .ct .hs .n.n.
根据您的评论,听起来您想要这样的东西:
#include <iostream>
#include <string>
#include <algorithm>
int main(int argc, char *argv[])
{
std::string input;
std::cin >> input;
std::transform (input.begin(), input.end(), input.begin(), tolower);
size_t i = 0;
while (i < input.length())
{
switch (input[i])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'y':
case 'u':
{
size_t pos = input.find_first_not_of("aeioyu", i+1);
if (pos == std::string::npos)
pos = input.length();
input.erase(i, pos-i);
break;
}
default:
{
input.insert(i, 1, '.'); // or: input.insert(i, ".");
i += 2;
break;
}
}
}
std::cout << input << std::endl;
return 0;
}
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 将 NULL 与 C 的字符* 字符串一起使用
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- 无法将常量字符字符串传递给模板类
- 如何组合一个宽字符字符串,中间插入一些空字符
- 如何将二维数组类型字符(字符串)作为函数参数传递?
- 从给定索引返回子字符字符串的函数
- 字符 [](c 字符串)的初始化标准
- 如何将字符字符串用作数学运算符
- 将 Unicode 字符/字符串写入文件
- C++,字符* 字符串修改
- 如何有效地用不同的整数元素替换字符字符串的元素
- C++:如果我们在字符串中添加一些整数,为什么它会从开头删除该数量的字符?(字符串 + 整数)
- strcpy正在复制sth字符i字符串.如何解决此错误
- 在C++中将双精度转换为字符*/字符串
- C 中的宽字符字符串
- 如何提取C 中的字符/字符串之间的字符串
- 无法将字符/字符串转换为int
- 带有指针的反转字符字符串
- 反转字符串中的 n 个字符(字符串中没有空格),而不使用 c++ 中的内置函数