Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

palindromes

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
ladyiou
Asked:
ladyiou
  • 9
  • 8
1 Solution
 
n_fortynineCommented:
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
 
ladyiouAuthor Commented:
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
 
n_fortynineCommented:
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
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!

 
n_fortynineCommented:
>>excepting
* laughing at myself... for this silly typo.
0
 
ladyiouAuthor Commented:
no i dont know how to get user input into a character array.
0
 
n_fortynineCommented:
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
 
ladyiouAuthor Commented:
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
 
n_fortynineCommented:
>>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
 
ladyiouAuthor Commented:
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
 
n_fortynineCommented:
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
 
ladyiouAuthor Commented:
all i did was type in anna and able was i ere saw elba
0
 
n_fortynineCommented:
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
 
ladyiouAuthor Commented:
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
 
n_fortynineCommented:
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
 
ladyiouAuthor Commented:
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
 
n_fortynineCommented:
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
 
ladyiouAuthor Commented:
no just wanting to learn just in case i decide to go to college sometime within the next two years.

thanks.
0

Featured Post

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!

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now