如何将点字符添加到字符串中的字符

How to add dot character to a character in string?

本文关键字:字符 字符串 添加      更新时间:2023-10-16

我想在字符串中的另一个字符旁边添加'.'字符,但我不知道该怎么做? 可能吗?

#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;
}