将字符插入字符串

时间:2014-01-23 00:20:39

标签: c++

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

#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] != '\0'; i++)
    {

        for (int j = 0; j < size; j++)
        {
            if (cad1[i] == vowels[j])
            {
                cad1.erase(cad1.begin() + i);

            }
        }

        for (int j = 0; cad1[j] != '\0'; j++)
        {
            cad1[j] = tolower(cad1[j]);
        }

        cad1 += ".";
        /* for (int k = 0; cad1[k] != '\0'; k++) {
                 if (k % 2 == 0) {
                     cad1.insert(k, 1, '.');       
                 } 

         } */
    }

    cout << cad1 << endl;
    cin.get();
}

3 个答案:

答案 0 :(得分:3)

之前你曾经有过的工作,但我想如果你包括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 = "BILLY\n";
    cout << name;
    remove_vowels(name);
    cout << name;
    to_lower(name);
    cout << name;
    put_dots(name);
    cout << name;
    return 0;
}

答案 1 :(得分:2)

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

#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] != '\0'; 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] != '\0'; j++)
// {
// }

//        cad1 += ".";
    for (int k = 0; cad1[k] != '\0'; k++) {
            if (k % 2 == 0) {
                cad1.insert(k, 1, '.');       
            } 
    }

    cout << cad1 << endl;
    cin.get();
}

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

如果您愿意,在寻找元音时,您可以实际转换为每个字符的小写字母。查看新编辑的代码。

答案 2 :(得分:0)

由于您已将其标记为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 again\n";
        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;
}

Example