Help with this one.
1.Write a program which will tell that entered string is palindrome or not. Make a function which takes a character array and also an integer array entered by user prints whether string is palindrome or not.
The problem is how to make the function of it?I just know how to take the string and have no clue how to reverse it and check it?any tips?
A palindrome
Started by ahmed, Nov 18 2008 05:05 AM
11 replies to this topic
#1
Posted 18 November 2008 - 05:05 AM
|
|
|
#2
Posted 18 November 2008 - 05:59 AM
Step 1: look up the string functions that are available. Also, the answer will depend a LOT on whether this is C or C++.
#3
Posted 18 November 2008 - 06:50 AM
offcourse i am using C++ , but i dont know how to make its function :(
#4
Posted 18 November 2008 - 08:20 AM
I would us rbegin and rend to create iterators to march through the string in opposite directions.
You can use 2 indices to do the same thing with the integer array.
You can use 2 indices to do the same thing with the integer array.
#5
Posted 18 November 2008 - 09:11 AM
The simpest way is to run a loop in both direction. One starting from 0 and second starting from length - 1. Compare symbols at each step and if they differs return false. Have you already made some code?
#6
Posted 21 November 2008 - 10:19 AM
I would just copy and reverse the string, and see it they are both equal.
Hope this helps
Graham - http://programmingtips.co.uk
Graham - http://programmingtips.co.uk
#7
Posted 21 November 2008 - 10:45 AM
My recommended approach:
- Have the function receive a const string.
- Allocate space for a copy of the incoming string.
- Copy the incoming string element by element as lowercase if it is alphanumeric. That is, skip whitespace and punctuation.
Consider the following as a test case.
const char one[] = "A Man, A Plan, A Canal-Panama!";
The lowercase alphanumeric-only copy would be:
amanaplanacanalpanama
- Then compare beginning and ending iterators, as mention by WingedPanther, incrementing the beginning and decrementing the ending until the beginning is past the ending. If the elements don't match, it's not a palindrome; if you make it all the way through, it is a palindrome.
#8
Posted 21 November 2008 - 12:27 PM
Re: dcf
You approach is simple, but contains a lot of needless work. For instance, you can iterate in both directions and skip punctuation "on the fly". Also, what for should you copy a string, if it isn't changed inside the function!?
You approach is simple, but contains a lot of needless work. For instance, you can iterate in both directions and skip punctuation "on the fly". Also, what for should you copy a string, if it isn't changed inside the function!?
#9
Posted 21 November 2008 - 12:31 PM
It is changed. You are removing punctuation and capitalization, so don't change the original string, allocate a copy!
#10
Posted 21 November 2008 - 02:48 PM
lucuis said:
You approach is simple, but contains a lot of needless work. For instance, you can iterate in both directions and skip punctuation "on the fly".
Frankly, if the OP would post an attempt, it would be easier to continue critiques in the direction you mention.
#11
Posted 29 November 2008 - 06:01 AM
this is my ultra failed attempt to just reverse the the char array hope u can help with it and il do with the rest of comparing
{
clrscr();
int i;
char array[10],t;
cout<<"\n Enter Name : ";
gets(array);
for(i=array[i];array[i]=<0;i++)
t=array[i-1];
cout<<array[i];
getch();
}
#12
Posted 29 November 2008 - 06:51 AM
JGRobinson said:
I would just copy and reverse the string, and see it they are both equal.
Don't do this. A more effective way is to loop through the character array and check the char at the beginning with the char at the end, and if at any point you find two chars that don't match return "false" and against the method.
Using the reverse method you are being wasteful as you are checking the entire string. If the first char at the beginning and the char at the end don't match then why check the rest of the string? You know it's not a palindrome.
bool isPalindrome(string sLine) {
int nFirst = 0;
int nHigh = sLine.length()-1;
while (nFirst < nHigh) {
if (sLine.at(nFirst) != sLine.at(nHigh)) {
return false;
}
nFirst++;
nHigh--;
}
return true;
}


Sign In
Create Account


Back to top









