Jump to content
  • Advertisement
Sign in to follow this  

need help with this simple c++ code

This topic is 3082 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi there,
I was making a program that could count the total number of palindromes in a sentence.
My instructor has advised us to use the 'string' class to make this program.
the idea of making such a program is that first I get a word from the sentence and then check whether it is a palindrome or not...

Here is the code:

#include <iostream>
#include <string>
using namespace std;

bool IsPalindrome(const string &word)
int size=word.length(), n=size/2;
bool equal=true;

for (int i=0, j=size-1; i<n && equal; i++, j--)
if (word!=word[j])

return equal;

bool IsAlphabet(const string &str, int index)
return ((str[index]>=65 && str[index]<=91) || (str[index]>=97 && str[index]<=123));

int get_palindromes(const string &sentence)
int num=0, j, size=sentence.length();
string word("nothing");

for (int i=0; i<size; i++)
if (IsAlphabet(sentence, i))
while (IsAlphabet(sentence, j+1))

word.insert(0, sentence, i, j+1);

// cout << word << endl;

if (IsPalindrome(word))


return num;

int main()
int n;
string str;

cout << "Enter a sentence: ";
getline(cin, str);


cout << "There are " << n << " number of palindromes. ";


return 0;

Input that I give is 'rotor and noon' or you can give any other input.. It does'nt work and I cant figure it out.. can someone please instruct what is going wrong here..?

Share this post

Link to post
Share on other sites
You don't mention what "doesn't work." Can you describe your problem?

Also, your definition of palindrome is incorrect. A palindrome is a sentence, not a list of words, in which the sequence of letters is the same forward and backward. You need to test the entire sentence (ignoring spaces) for that property.

"A man, a plan, a canal, Panama" is a palindrome but no individual word is palindromic.

You can also make use of the string functions such as first_of, first_not_of, etc., which may make things a bit easier.

Share this post

Link to post
Share on other sites
Did you try debugging the program?

Also, can you explain the algorithm you're using?

Share this post

Link to post
Share on other sites
thanks for the reply,

In this program I am trying to count total number of words that are palindrome, E.g.

'rotor and noon'

there are 2 palindromes in this sentence, first 'rotor' and then 'noon'..
I am not checking wether the whole sentence is a palindrome or not...

the problem in the code is that, When I input 'rotor and noon'
the first time string 'word' takes 'rotor' from the string 'sentence' successfully and the checks that whether its a palindrome or not...

then second time, word should take 'and' but it takes 'and noon' which I do not understand why?? I even debugged it...

can someone please debug it for me?? and tell what is wrong in the code??

I think it should work......

Share this post

Link to post
Share on other sites
Original post by Ozair
I even debugged it...

When debugging, pay close attention to how variables are modified, and if they are getting the values you think they should be getting.

Also, try thinking about your logic. How do you extract a word from a sentence?

Share this post

Link to post
Share on other sites
my algorithm is that first I try to find a single character that is a alphabet then I enter a loop when counts till the end of a alphabet, hence I get total number of characters in a word, then I insert it in a seperate string and check whether it is a palindrome or not and then I repeat the whole scenario until the sentence finish..

now the problem is that... at 2nd iteration of the main loop the string 'word' has 'and noon' where it should have 'and' only, the values of the variables that are being modified are correct, I can't understand...

Can someone please help me by debugging it yourselves??

Share this post

Link to post
Share on other sites
The forum rules state that we can't do homework for other people, we can only help them do it themselves.

So, here are a few suggestions:

Instead of using erase() and insert(), use substr(), it's more convenient here. It returns a new string that is a substring of the string it's called on. It takes two arguments: the starting index, and the length of the substring.

To use it here, you need to find the index where a word begins, the word's length, and then pass those as parameters to substr().

So you need to figure out two things:

1) How to find the beginning of a word. (that is, the index of the first character in the word)
2) How to find the word's length.

You'll need to repeat this for every word in the sentence, so once you find a word, you need to figure out where to start looking for the next word.

I'll also repeat what I said earlier - when debugging, watch how the variables change values. Before executing a line of code that changes a variable, try to predict what the variable's new value will be after the line is executed. Since the code is incorrect, at some point a variable will not get the value you expected. When that happens, think carefully about how the wrong value came to be.

Do this repeatedly, and eventually you'll find the problem. Read this for more info.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!