Solved

palindromes

Posted on 2003-11-23
17
478 Views
Last Modified: 2010-04-15
I ONLY HAVE FIFTY PTS LEFT AND I AM WILLING TO GIVE THEM TO THE PERSON WHO COMES UP WITH THE BEST SOLUTION BUT THE QUESTION IS EXTREMELY IMPORTANT TO ME. SO IF THERE IS ANYONE OUT THERE WHO DOESNT MIND HELPING REGARDLESS OF THE PT VALUE I THANK YOU KINDLY.

Does anyone have the code for writing a program that will prompt the user for a word or phrase and will then store the input into a character array.  Once the word or phrase has been retrieved from the user, then write a code that will determine if the word or phrase is a palindrome (i.e. does it spell the same thing forwards and backwards, examples of palindromes are “sos”, “radar”, “able was I ere I saw elba”, “123454321” ).  Once the code has determined if the input character string is a palindrome, it should notify the user accordingly?
0
Comment
Question by:ladyiou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9806708
ladyiou, we do not help you because of the amount of points we would potentially received (ehem, well, most of us), and we would be glad to assist you, and point out what coding errors you're making or help solving any conceptual roadblocks you're running into, but at least we MUST see some effort being made. This sounds awfully like a class assignment to me. Why don't you give it a try, post some code, and see where we'll go from there.

And note that though there might be many ways to solve any problems, THE BEST SOLUTION has to depend on whether you would be ready to do it the "better" way (using more complicated concepts) or the hard way (using simpler concepts) first. Your professor wouldn't want to see his CS 101 (or whatever) student submitting something that is suspiciously above that level.
0
 

Author Comment

by:ladyiou
ID: 9806825
i understand but this isnt an assignment and i am not in school. i have a book by deitel & deitel that says write the program and i just wanted some help in figuring it out since i dont know much about C. This is the language that i am looking into. thanks for your advice though.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9806923
Well, in that case... I assume you know how to get the user's input into a character array. Your problem is to come up with an algorithm to determine whether a string is a palindrome or not.

A simple solution is to determine the length of the string, and then proceed from the beginning AND the end simultaneously, while comparing the corresponding characters until you reach the middle of the string.

So say you have the array "123454321". The length of this array is 9.
Now you proceed with two counters. One starts at position [0] and the other at [8] (the last position).
You can easily see how the work is done now.
Compare '1' with '1'. Good?
Move the front counter up and the back counter down.
Compare '2' with '2'. Good?
Move the front counter up and the back counter down.
etc.
Until the front counter is either equal or greater than the back counter. At that moment you're at the middle of the array. If you ever get to this point that means all previous comparisons are OK, then you'll have a palindrome.

Now consider "123454621". Proceed as above.
When you reach the third step. Comparing '3' and '6'. Oops. They're not equal. Then right here your work is done, and you know this is NOT a palindrome. You can quit comparing here.

The steps above can be implemented using a loop, since as you've probably noticed we're just doing the same thing over and over again, excepting for incrementing and decrementing the counters.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:n_fortynine
ID: 9806931
>>excepting
* laughing at myself... for this silly typo.
0
 

Author Comment

by:ladyiou
ID: 9807158
no i dont know how to get user input into a character array.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9807206
You can use scanf( ). A more detailed discussion can be found here:
http:Q_10084593.html
And check out at the example from the accepted answer (the string part).
0
 

Author Comment

by:ladyiou
ID: 9819353
here is some part of a code that i did, but now what do i do. i know i want to do counter i guess with a for loop that increments by one starting with the first letter and decrement by one starting with the last letter.

CODE:

#include <stdio.h>

int main(void)
{
  char string[40];
  char temp;
  int counter = 0;
  int i;
  printf("Please enter a potential palendrome\n");
 
  scanf("%c", &temp);
  while ((temp != '\n') && (counter < 40))
  {
    string[counter] = temp;
    counter++;
    scanf("%c", &temp);
  }
  string[counter] = '\0';

  /*for(i=0; i<counter;i++)
     printf("%c", string[i]);
  printf("\n");*/
  printf("%s\n", string);

  return 0;
}

This is not working properly so what should i add or take away.  some major help needed here.
0
 
LVL 4

Accepted Solution

by:
n_fortynine earned 50 total points
ID: 9819625
>>i know i want to do counter i guess with a for loop that increments by one starting with the first letter and decrement
>>by one starting with the last letter
right.

#include <stdio.h>

void main(void) {
  char string[40], temp;
  int counter = 0, i, j;
  printf("Please enter a potential palindrome\n");

  scanf("%c", &temp);
  while ((temp != '\n') && (counter < 40))
  {
    string[counter] = temp;
    counter++;
    scanf("%c", &temp);
  }
  string[counter] = '\0';

  for(i = 0, j = counter - 1; i < j; ++i, --j)
     if(string[i] != string[j]) {
        printf("%s is not a palindrome\n", string);
        return;
     }

  printf("%s is a palindrome\n", string);
}

Note that you will need some slight modification to make this, say, loop around to ask for more strings, or check for strings with spaces and punctuations.

Let me know if this helps.
0
 

Author Comment

by:ladyiou
ID: 9821708
Why do i get
 

^U
  B^U@aO^A@oyc?(foreign and upside down question mark>
                            I^D@^A is a palindrome


when i put in the user input (word palindromes not numbers)?

i am sorry about asking so many questions but i am getting frustrated because i am not getting this and i am about to just call it quits.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9821807
What did you type in for the input? I just tested the code above on GCC compiler and it works (no doubt in VC++ as well). Please elaborate some more.

Programming takes time and effort. I personally did not pick up programming until I get into college (that is 2 and 1/2 years ago) and I can say it's not an easy trip, but with hard work does pay off. So don't get frustrated when you don't understand things. :)
0
 

Author Comment

by:ladyiou
ID: 9821960
all i did was type in anna and able was i ere saw elba
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9821972
anna and able was ere saw elba both worked for me (using the same code I posted 3 posts back). What compiler are you using?
0
 

Author Comment

by:ladyiou
ID: 9821990
i am using pico which is from pine and uses scc -o to compile and execute.  for string[counter] = '\0';    i typed in '\n'; instead. could this be a problem?
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9821999
That '\n' should be a problem because there would be no termination for the string. Glad you fixed that.
Hey, I also do all my composing and compilation with pico and pine-rc. =)
0
 

Author Comment

by:ladyiou
ID: 9822007
cool. thanks for all your help.


do you know anyone that can help me with a game that i am trying to create.
GAME -- 3x3 tic tac toe with two players and then against the computer using 2 dimensional arrays?
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9822068
I'll be glad to do so. If you have any problem you can email me at n_fortynine@yahoo.com (since you've stated you have no points left to ask at EE) - and my help is free =) It's Thanksgiving break over my school anyway. I am not proficient with AI techinques but I've done something like that before (as I could recall) a simple AI version of 3x3 tictactoe.

A small advice. Are you learning C for any special purposes? C++ would be a better place to start than its predecessor.
0
 

Author Comment

by:ladyiou
ID: 9822370
no just wanting to learn just in case i decide to go to college sometime within the next two years.

thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question