Jump to content

A palindrome

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
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?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Step 1: look up the string functions that are available. Also, the answer will depend a LOT on whether this is C or C++.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
offcourse i am using C++ , but i dont know how to make its function :(

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
lucuis

lucuis

    Newbie

  • Members
  • PipPip
  • 21 posts
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
JGRobinson

JGRobinson

    Newbie

  • Members
  • PipPip
  • 22 posts
I would just copy and reverse the string, and see it they are both equal.
Hope this helps
Graham - http://programmingtips.co.uk

#7
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
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
lucuis

lucuis

    Newbie

  • Members
  • PipPip
  • 21 posts
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!?

#9
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
It is changed. You are removing punctuation and capitalization, so don't change the original string, allocate a copy!
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#10
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

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".
True enough, but more complex. As this is usually a beginner's exercise, my recommendation was not an exercise in efficiency -- rather one in which the basics are more approachable. After the copy, the single-case no punctuation version can be printed for easier debugging, for example. Making a copy is a safeguard in case the function is passed a string literal.

Frankly, if the OP would post an attempt, it would be easier to continue critiques in the direction you mention.

#11
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
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
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts

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;

}