• 13
• 14
• 27
• 9
• 9

# need help with this simple c++ code

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

## 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])			equal=false;	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))		{			j=i;			while (IsAlphabet(sentence, j+1))				j++;			word.erase();			word.insert(0, sentence, i, j+1);		//	cout << word << endl;			if (IsPalindrome(word))				num++;			i=j;		}		return num;}int main(){	int n;	string str;	cout << "Enter a sentence: ";	getline(cin, str);	n=get_palindromes(str);	cout << "There are " << n << " number of palindromes. ";	cin.get();		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 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.

E.g.,
"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 on other sites
Did you try debugging the program?

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

##### Share on other sites

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 on other sites
Quote:
 Original post by OzairI 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 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...

##### 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.