回文计划没有正确比较

时间:2016-05-02 04:20:56

标签: c++ string vector

我目前正在学习矢量并尝试使用它们制作回文程序。这是一个简单的程序,到目前为止,我试图让它识别"我就是我。"作为回文正确。到目前为止,这是我的计划:

#include <vector>
#include <string>
#include <iostream>

using namespace std;

vector <string> sentVec;

void getSent(string sent); 
void readBackwards(string sent); 

int main()
{
 string sent;

 getSent(sent);
 readBackwards(sent);
 return 0;
}

void getSent(string sent)
{
 cout << "Enter your sentence:" << endl;
 getline (cin,sent);

 string currentWord, currentLetter;

 for (int i = 0; i < sent.length(); i++) 
 {
  currentLetter = sent[i];

  if (currentLetter == " ") // inserts word
  {
   currentWord += sent[i];
   sentVec.push_back(currentWord);
   currentWord = "";
  }
  else if (currentLetter == ".") // inserts period
  {
   sentVec.push_back(currentWord);
   currentWord = sent[i];
   sentVec.push_back(currentWord);
  }
  else
  {
   currentWord += sent[i];
  }
 }
}

void readBackwards(string sent)
{
 string sentForwards, sentBackwards; 

 // create sentence forwards and backwards without the period.
 for (int i = 0; i < sentVec.size() - 1; i++)
 {
  sentForwards += sentVec[i];
 }

 for (int j = sentVec.size() - 2; j >= 0; j--)
 {
  sentBackwards += sentVec[j];

  if (j == sentVec.size() - 2)
   {
    sentBackwards += " ";
   }
 } 

 cout << "Sentence forwards is: " << sentForwards << endl;
 cout << "Sentence backwards is: " << sentBackwards << endl;

 if (sentForwards == sentBackwards)
 {
  cout << "This sentence reads the same backwards as forwards." << endl;
 }
 else 
 {
  cout << "This sentence does not read the same backwards as forwards." << endl;
 }
}

当我运行此程序时,它会打印:

Enter your sentence:
I am what am I.
Sentence forwards is: I am what am I
Sentence backwards is: I am what am I 
This sentence does not read the same backwards as forwards.

为什么在比较两个句子时这不会触发if循环?

3 个答案:

答案 0 :(得分:2)

由于sentBackwardssentForwards不同,因为sentBackwards在末尾有一个尾随空格,因此它们不相同。

答案 1 :(得分:1)

我不确定你的程序如何检测回文,但这是一个简单的迭代方法:

action.php
session_start();
$_SESSION['title']= htmlspecialchars($_POST['title']);
// all the other variables


video.php
session_start();
echo $_SESSION['title'];

如果作为参数传递的字符串是回文

,则返回true

答案 2 :(得分:1)

您不仅应该了解vector,还应该了解std::reverse等STL算法函数。

正如给出的另一个答案所指出的,一个向量具有尾随空格。你可以通过简单地取出原始矢量,将其复制到另一个矢量并调用std::reverse来避免所有这些。没有必要写一个循环:

void readBackwards()
{
   // copy the vector
   std::vector<std::string> sentBackwards = sentVec;

   // reverse it
   std::reverse(sentBackwards.begin(), sentBackwards.end());

   // see if they're equal
   if (sentVec == sentBackwards)
      cout << "This sentence reads the same backwards as forwards." << endl;
   else 
      cout << "This sentence does not read the same backwards as forwards." << endl;
}

这是有效的,因为std::vector有一个重载operator ==,它比较两个向量中每个向量中的项目,如果所有项目都相同则返回true

除此之外,阅读载体可以比你尝试的更容易完成。

#include <sstream>
#include <algorithm>
//...
void getSent(string sent)
{
    // remove the periods(s)
    auto iter = std::remove_if(sent.begin(), sent.end(), [] (char ch) { return ch == '.';});
    sent.erase(iter, sent.end());

    // copy the data to a vector
    std::istringstream iss(sent);
    string currentword;
    while ( iss >> currentword)
        sentVec.push_back(currentword);
}

请注意,我们使用std::istringstream作为空格分隔的解析器,减少了编写寻找空间的循环的需要。此外,在我们开始将各个字符串存储到向量中之前,std::remove_if算法用于从字符串中删除任何句点字符。

基本上,整个设置中唯一的循环是从流中读入向量的while。其他一切都是通过使用算法函数完成的,并利用std::vector的各种成员函数(如重载==