2014-01-23 62 views
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] != '\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(); 
} 
+2

你为什么不使用'的std :: string'?我看到你收录了它。 – 735Tesla

+1

关于你的辅音问题 - 如果角色不是元音,它是辅音,对吗?这应该有助于你找到那些。 – Nabren

+0

对不起,我的第一个答案,我误解了你的问题。我会稍微改变一下,然后再发布一次。 – 735Tesla

回答

2

你错位了最后一个循环。它应该在外循环之外。请参阅我下面的代码进行的修改:

#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循环的字符串。查看新的编辑后的代码。

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

+0

现在是另一个问题。该代码不会擦除字母'Y','y'或'o'。它的奇怪,因为对于输入:“Codeforces”它消除了'o'。 – user3152299

+0

如果有2个元音在一起,则会发生错误。指针在删除第一个元音之后继续保留第二个元音。我会更新代码来解决这个错误。 – alvits

+0

解决方案非常简单。更改'cad1.erase(cad1.begin()+ i);'减少'i',因此指针将在删除后停留在新字符处。 'cad1.erase(cad1.begin()+ i - );' – alvits

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

虽然这通常会起作用,但如果'char'不符合':: tolower'参数的要求(例如,如果它超出范围),那么使用带'char'输入的':: tolower'可以具有UB [0,CHAR_MAX]')。 C++版本可以在''中找到。 –

+0

@ZacHowland好的,谢谢你的信息。我想我现在知道我的另一个节目有什么问题。我会更新我的答案。 – 735Tesla

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