10 Replies - 5365 Views - Last Post: 17 July 2011 - 10:48 PM Rate Topic: -----

#1 Mercury 1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-July 11

Palindrome Array

Posted 17 July 2011 - 10:51 AM

Problem: Write a C++ function that takes a char array & tests if the array is a palindrome or not...
# include < iostream >
# using namespace std; 
bool palindrome (char ar [], int size)
{
if (char 'ar==reverse(int size)')
return(true);
else
return(false);
}
void main ()
{
char arr [5] = "BabybaB";
cout<< palindrome (arr,5); // will print 1 because the array is read from left to right and right to left (palindrome)

char arr[5] = "Time";
cout<< palindrome (arr, 4); 
}


I got many syntax error & I need a little help...

This post has been edited by macosxnerd101: 17 July 2011 - 10:56 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Palindrome Array

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Palindrome Array

Posted 17 July 2011 - 11:03 AM

First please use code tags when posting code!

Second if you received an error please post the complete error message exactly as it appears in your development environment.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Mercury 1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-July 11

Re: Palindrome Array

Posted 17 July 2011 - 11:11 AM

First I'm sorry but this the first time for me participating...
Second: The error msgs are
error C2143: syntax error : missing ',' before 'constant'
error C2181: illegal else without matching if
error C2601: 'main' : local function definitions are illegal
cpp(6): this line contains a '{' which has not yet been matched
error C2117: 'arr' : array bounds overflow
cpp(15) : see declaration of 'arr'
error C2117: 'arr' : array bounds overflow
cpp(18) : error C2374: 'arr' : redefinition; multiple initialization
see declaration of 'arr'
Thanx in advance...
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Palindrome Array

Posted 17 July 2011 - 11:29 AM

Your first error is being caused by this line: # using namespace std; . You do not want the #, the # denotes a pre-processor directive and using namespace is not a pre-processor directive.

Next in this line: if (char 'ar==reverse(int size)') you have several errors. First the use of the single quotes signifies a character constant. A character constant is one character, not multiple characters. Also I am not too sure what you are actually trying to accomplish with this statement.

Also you are trying to define variables with the same name within the same scope, that is not allowed.

Finally in C++ main must be defined to return an int: int main().


Jim
Was This Post Helpful? 1
  • +
  • -

#5 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Palindrome Array

Posted 17 July 2011 - 11:32 AM

if (char 'ar==reverse(int size)')

I'm not sure what this is supposed to do, especially the apostrophe's. Could you explain it?

# using namespace std; 

using namespace shouldn't have a '#' in front of it.

Last but certainly not least...

You should never use void main. The main is an integer function which returns an integer.

This post has been edited by Aphex19: 17 July 2011 - 11:34 AM

Was This Post Helpful? 1
  • +
  • -

#6 Mercury 1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-July 11

Re: Palindrome Array

Posted 17 July 2011 - 11:46 AM

Actually I'm not sure of my code since I'm still a beginner in C++ Coding, But I know that a palindrome array is read from left to right also from right to left... so I'm just trying to apply this on C++ with no use, I've tried several codes before this but nothing worked... :(
Anyway I'm trying to use the (y) letter in "BabybaB" as the middle one since the word is read from both sides (palindrome) But I'm not sure how to do that...

The void main () is included in the problem itself not in my code, although I know it returns no value...
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Palindrome Array

Posted 17 July 2011 - 12:40 PM

First void main() is wrong it doesn't matter where it came from it is wrong. See this link: Things to Avoid in C/C++.

Next I suggest that you start with by re-reading your text book on how a C/C++ program is constructed. This might be a place to start: C++ Tutorial. Start with the links under the "Basics of C++".

Jim

This post has been edited by jimblumberg: 17 July 2011 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

#8 Mercury 1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-July 11

Re: Palindrome Array

Posted 17 July 2011 - 12:47 PM

rereading!!! It's the 100000000 time I read this subject, but it's such a difficult material & very complicated to construct such a program, Anyway thanx a lot for your efforts...
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Palindrome Array

Posted 17 July 2011 - 01:08 PM

I suggest that you start with a very simple program. Maybe start with the most basic of programs. Write a program that outputs a C-string (null terminated character string) variable with the name of myVar that contains "Hello World" to the screen. Compile and run the program. Does the program compile and run without any errors? If it does not compile without errors and you don't understand the errors then post the code causing the problems along with the entire error message exactly as it appears in your development environment, along with questions based on the posted code.


Jim
Was This Post Helpful? 0
  • +
  • -

#10 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Palindrome Array

Posted 17 July 2011 - 08:29 PM

Having taken note of the above information and advice:
Note that in C++ BacybaB is an automatically terminated '\0' string. That is
string s = "BabybaB"

s is also an array so
if (s[0]==s[6] && s[1]==s[5] && s[2] == s[4])//s is a palindrome

also
 s.length()//will provide you with the length of the string if you need it.

Using a pencil and paper write out the method (algorithm) to follow which will produce the required result/s.
Now write the code in accordance with this method and post it here if you have other difficulties.
good luck
Was This Post Helpful? 0
  • +
  • -

#11 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Re: Palindrome Array

Posted 17 July 2011 - 10:48 PM

well Mercury1 , seeing that you tried , here i could give you this hint

int chkpalindrome(char str[])
{
   int len=0;
   while (str[len])
   len++;
   int left=0, right=len-1, palin=1;
   while (left<right)
      if (str[left]==str[right] && palin==1)
      {
          left++;
          right--;
      }
      else
          palin=0;
   return palin;
}
void main()
{
    char a[]="RacecaR";
    int x = chkpalindrome(a);
    if(x==1)
       cout<<"Palindrome\n";
    getch();
}



Hope that helps , i appreciate your effort even though it was little.
Next time write out your desired output on a piece of paper and try getting it some way or the other.

this program can also be done in another way
you can compare the string that you have given by reversing it using strcmp(); and if case has to be ignored use sricmp();

void main()
{
    char a[]="BabybaB";
    char temp[100];
    strcpy(temp,a);
    if(strcmp(strrev(a),temp)==0)
    	cout<<"Palindrome\n";
    else
    	cout<<"Not palindrome\n";
    getch();
}


Was This Post Helpful? -2
  • +
  • -

Page 1 of 1