将字符插入字符串

Inserting a character into a string

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

我已经尝试了此站点上的其他帖子,但它没有用。我希望你能帮助我。问题是要删除给定字符串中的所有元音,然后将其转换为小写,最后在每种辅音之前插入'.'字符。最后一个是给我麻烦的一个。

#include <iostream>
#include <cstdio>
#include <ctype.h>
#include <string>
using namespace std;
string cad1;
char vowels[] = {
    'A', 'O', 'Y', 'E', 'U', 'I', 'a', 'o', 'y', 'e', 'u', 'i'
};
int size = sizeof(vowels) / sizeof(vowels[0]);
string ToLowerCase(string text)
{
    for (int i = 0; i < text.length(); i++)
    {
        char c = text[i];
        if ((c >= 65) && (c <= 90))
        {
            text[i] |= 0x20;
        }
    }
    return text;
}

int main()
{
    cin >> cad1;
    for (int i = 0; cad1[i] != ''; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (cad1[i] == vowels[j])
            {
                cad1.erase(cad1.begin() + i);
            }
        }
        for (int j = 0; cad1[j] != ''; j++)
        {
            cad1[j] = tolower(cad1[j]);
        }
        cad1 += ".";
        /* for (int k = 0; cad1[k] != ''; k++) {
                 if (k % 2 == 0) {
                     cad1.insert(k, 1, '.');       
                 } 
         } */
    }
    cout << cad1 << endl;
    cin.get();
}

您以前的工作将有效,但是我认为,如果您包括std::string,则应使用其功能。尝试以下操作:

#include <iostream>
#include <string>
#include <locale>
using namespace std;
// All of the vowels to remove
const string vowels = "aeiouyAEIOUY"; // I don't know if you actually want y here
// Takes a reference to the string you give it
void remove_vowels(string &orig_str) {
    // Iterate through the string, remove all of the characters in out vowels string
    for (int i=0; i<orig_str.length(); ++i) {
        orig_str.erase(remove(orig_str.begin(), orig_str.end(), vowels[i]), orig_str.end());
    }
}
// this function is pretty self explanatory 
void to_lower(string &orig_str) {
    transform(orig_str.begin(), orig_str.end(), orig_str.begin(), ::tolower);
}
void put_dots(string &orig_str) {
    // it is important to define max before you increase the length of the string
    int max = orig_str.length();
    // iterate through the string, inserting dots
    for (int i=0; i<max; ++i) {
        orig_str.insert(i, string("."));
        // we want to increase i again because we added a character to  the string
        ++i;
    }
}
int main(int argc, const char * argv[])
{
    string name = "BILLYn";
    cout << name;
    remove_vowels(name);
    cout << name;
    to_lower(name);
    cout << name;
    put_dots(name);
    cout << name;
    return 0;
}

您放错了最后一个循环。它应该在外部循环外。请参阅下面对您的代码进行的修改:

#include <iostream>
#include <cstdio>
#include <ctype.h>
#include <string>
using namespace std;
string cad1;
char vowels[] = {
    'A', 'O', 'Y', 'E', 'U', 'I', 'a', 'o', 'y', 'e', 'u', 'i'
};
int size = sizeof(vowels) / sizeof(vowels[0]);
string ToLowerCase(string text)
{
    for (int i = 0; i < text.length(); i++)
    {
        char c = text[i];
        if ((c >= 65) && (c <= 90))
        {
            text[i] |= 0x20;
        }
    }
    return text;
}

int main()
{
    cin >> cad1;
    for (int i = 0; cad1[i] != ''; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (cad1[i] == vowels[j])
            {
                cad1.erase(cad1.begin() + i);
            }
        }
       cad1[i] = tolower(cad1[i]);
    }
// for (int j = 0; cad1[j] != ''; j++)
// {
// }
//        cad1 += ".";
    for (int k = 0; cad1[k] != ''; k++) {
            if (k % 2 == 0) {
                cad1.insert(k, 1, '.');       
            } 
    }
    cout << cad1 << endl;
    cin.get();
}

实际上,将字符串转换为小写的循环也应移出外部循环。请参阅新的编辑代码。

如果您愿意,实际上可以在寻找元音时穿越它们时将每个字符转换为小写。请参阅新编辑的代码。

,因为您已将其标记为C ,该解决方案使用标准库的算法:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <limits>
#include <locale>
#include <sstream>
#include <string>
const std::string VOWELS = "AEIOUY"; // assume Y is always a vowel
int main()
{
    // read the input
    std::string input;
    while (!(std::cin >> input))
    {
        std::cout << "Invalid input, try againn";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
    }
    // erase vowels
    input.erase(std::remove_if(input.begin(), input.end(), [&](char c)
    {
        std::locale loc;
        c = std::toupper(c, loc);
        return std::find(VOWELS.cbegin(), VOWELS.cend(), c) != VOWELS.cend();
    }), input.end());
    // transform to lowercase
    std::transform(input.begin(), input.end(), input.begin(), [](char c)
    {
        std::locale loc;
        return std::tolower(c, loc);
    });
    // add . before every remaining letter
    std::ostringstream oss;
    std::for_each(input.begin(), input.end(), [&](char c)
    {
        std::locale loc;
        if (std::isalpha(c, loc))
        {
            oss << ".";
        }
        oss << c;
    });
    input = oss.str();
    std::cout << "Resulting string:  " << input << std::endl;
    return 0;
}

示例