0

I'm trying to get a sentence delimited by certain characters (either a space, comma, or a dot) to check if it's a palindrome. If the input is "hello,potato.", I'll check this symmetry on "hello" alone and then potato alone.

The problem is, while I'm doing the first iteration of the loop that searches for the delimiter, the word "hello" is stored in the sub-sentence, but on the second iteration the word that should be stored as "potato" will be "potato.". And I am unable to remove the "." delimiter from the end of the input string.

for(int i=0;i<sentence.length();i++)
      {
        if(sentence[i]==' '||sentence[i]=='.'||sentence[i]==',')
        { //the couts is just to help me debug/trace
                cout<<"i is now : "<<i<<endl;
                if(i==delindex && i==sentence.length()-1)
                {
                 subsentence=sentence.substr(temp+1,subsentence.length()-1);
                }
                else
                {
                    subsentence=sentence.substr(delindex,i);
                    cout<<subsentence<<endl;
                    temp=delindex-1;
                    delindex=i+1;
                }
        }
      }

What would be the best way to go about this?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    @JCollier its bocmes like potato. with a dot in the end(the delimiter) i wanted without the dot – Mostafa Anwar Mar 05 '18 at 21:36
  • While there is a very simple way to use `.begin()` and `.rbegin()` to deduce a palindrome, parsing on multiple delimiters is still best handled by [strtok](http://en.cppreference.com/w/cpp/string/byte/strtok) provided by ``. While `getline` allows an ending delimiter, it doesn't allow multiple delimiters. – David C. Rankin Mar 05 '18 at 21:43
  • @DavidC.Rankin god bless you man that strtok is what i have been looking for – Mostafa Anwar Mar 05 '18 at 22:22
  • It is the greatest thing since sliced-bread for tokenizing a string (there a a number of nuggets to be found in the `` header that don't have a direct class counterpart in C++) – David C. Rankin Mar 05 '18 at 22:25

1 Answers1

2

god bless you man that strtok is what i have been looking for

Actually, you don't need strtok (and should probably avoid it for various safety reasons), as std::string has a wonderful method called find_first_of which acts pretty much like strtok, as in it accepts a bunch of chars and returns index when it stumbles on any of the chars. However to make robust tokenizer a combination of find_first_of and find_first_not_of is more suitable in this case.

Therefore you could simplify your token searching to:

#include <iostream> 
#include <string>

int main()
{
    std::string sentence = "hello,potato tomato.";
    std::string delims = " .,";

    size_t beg, pos = 0;
    while ((beg = sentence.find_first_not_of(delims, pos)) != std::string::npos)
    {
        pos = sentence.find_first_of(delims, beg + 1);
        std::cout << sentence.substr(beg, pos - beg) << std::endl;
    }
}

https://ideone.com/rhMyvG

Killzone Kid
  • 6,171
  • 3
  • 17
  • 37