Link to home
Start Free TrialLog in
Avatar of mbrinks3
mbrinks3

asked on

Palindrome Help!!

I need to write a C program that takes a string as input and validates if the string is a palindrome. I have written part of the code, but have missing pieces. I think I need a "for" statement, but am not sure how to write or incoprortae it.

This is what I have - any help would be appreciated:

#include <stdio.h>

main(void)

{

char string1[25], sample;
int counter=0, a, b;
printf("Palidrome Word Check\n")
prinff("Enter the word you would like to be checked:\n");
scaf("%s",&sample);
while sample != (counter <25)
{
string(counter)=sample;
counter++;
scanf("%s",&sample);
}

if string[a]!= string[b]
{
printf ("%s is not a palidrome\n",string);
}
{
printf ("%s is a palindrome\n",string);

printf("\n\n");      
printf("Press any key to end program");
    getch();
   
}
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Tip: Reverse the input string, then compare it to the input - if they match, you found a palindrome. E.g. like:

void reverse_string ( char* in, char* out) {

    int nSize = strlen(in);

    for ( int i = 0; i < nSize; ++i) {out[i] = in[nSize - i - 1];}

    out[nSize + 1] = '\0';
}

Oh, yes, and remove the myriad of errors in your code or don't write "have written part of the code" without blushing ;o)
Avatar of mbrinks3
mbrinks3

ASKER

Thanks for the comments. When I try and complie this is all that happens:

Miracle C Compiler (r3.2), written by bts.
Compiling c:\program files\miracle c\week5.test.c

Any thoughts?
> Any thoughts?

Find a better compiler. Miracle C is able to compile and run the program below without errors or warnings. It is very difficult learning C if you don't know what is wrong...

#include <stdio.h>

void func(float f)
{
  printf("%f\n",f);
}

int main()
{
  func(2.2);
  func(4);
  func("test");
  return 0;
}

output:
2.200000
0.000000
0.000000
Because this is a class we can only use Miracle C. Help!!!! I need a miracle about now...
But can you create your program with another compiler. When it's finished, you probably do not need to change anything to compile it in miracle c too.
A more informative compiler says

"mbrinks3.c", line 10: error(1065): expected a ";"
  prinff("Enter the word you would like to be checked:\n");
  ^

"mbrinks3.c", line 12: error(1124): expected a "("
  while sample != (counter <25)
        ^

"mbrinks3.c", line 14: error(1133): expression must be a modifiable lvalue
  string(counter)=sample;
  ^

"mbrinks3.c", line 19: error(1124): expected a "("
  if string[a]!= string[b]
     ^

At end of source: warning(1012): parsing restarts here after previous syntax
          error

At end of source: error(1029): expected an expression

At end of source: error(1018): expected a ")"

At end of source: error(1029): expected an expression

At end of source: error(1065): expected a ";"

At end of source: error(1067): expected a "}"
The biggest tip I can give you is this:

while sample != (counter <25)
{
string(counter)=sample;
counter++;
scanf("%s",&sample);
}

can be replaced with

for(counter = 0 ; counter < strlen(sample); counter ++)
   string1[counter] = sample[(strlen(sample) - 1) -counter];


as for this line: if string[a]!= string[b]

you are comparing a character to another character, where you should be comparing 2 strings as a whole. To compare 2 string, you should use int strcmp(char * a, char * b), this returns 0 if string a and b are the same.

hope this helps.

I took some sdvice from sunnycoder and basically started over - I think I am closer, but still not there.

New program

*Laurie Meissner*/
/*Week 5 Assignment - Palindrome Test*/
/*December 18,2005*/

#include <stdio.h>

main()
{
/*Create character string*/
  char string[20], test;
  int count = 0;
  int a, b;
 
/*Get user input*/  
  printf("Palindrome Word Test\n\n");
  printf("Please enter a word to be tested\n");
  scanf("%s", &test);
 
/*Test characters to see if they are equal*/  
  {
   while ((test != count) & (count < 20));
   string[count] = test;
   count++;
   scanf("%s", &test);
}
   string[count] = 0;

   for(a = 0, b = count - 1; a < b; ++a, --b)
   if(string[a] != string[b])
{
/*Print output*/  
   printf("%s is not a palindrome\n\n", string);
   
   printf("Press any key to end program");
   getch();
   return(0);
}
 
  printf("%s is a palindrome\n", string);

  printf("\n\n");      
  printf("Press any key to end program");
  getch();
}        
 
OK, that looks better - now, have you taken a look at what I suggested? When you reverse a string and it matches the original (hint: 'strcmp()') you found a palindrome...
Ok ... now lets try to get this code working ...
> scanf("%s", &test);
test is a char ... and you are trying to read a string into it .... You will write the string beyond the char and corrupt memory ...
You need to have proper storage for the string ... a char array would be more appropriate to read in a string.

/*Test characters to see if they are equal*/  
  {
   while ((test != count) & (count < 20));
   string[count] = test;
   count++;
   scanf("%s", &test);
}

I am not sure what you are trying to accomplish here ... The statement block enclosed in { } should be after while statement ... The semi-colon after the while statement would terminate the loop then and there ... hence all following statements would be executed only once. The general syntax of while loop is
while (condition)
{
      //block of statements which have to be looped through
}

If you are familiar with the pointers, you wont actually need to copy the string ... you can maintain a pointer to the beginning and end of the string and advance them while comparing the contents. If you have not lerant pointers yet, then saving a reversed copy of the string would be required.

Cheers!
sunnycoder
I'd rather just construct the string in reverse and then compare the 2 strings. Seems like what you're trying to do is compare the individual characters until u reach the middle of the word. That works fine also, but a little bit more complicated.
eg:

for(counter = 0 ; counter < strlen(sample); counter ++)
   string1[counter] = sample[(strlen(sample) - 1) -counter];

Done deal.
Hey there,
                  works perfectly..gimme the 500 points mate..
$tranger.

<Source code removed by Page Editor. > 
Kelvin_King,

>>I'd rather just construct the string in reverse and then compare the 2 strings.

What do you think I was referring to with

------------------------------------------->8----------------------------------

Tip: Reverse the input string, then compare it to the input - if they match, you found a palindrome. E.g. like:

void reverse_string ( char* in, char* out) {

    int nSize = strlen(in);

    for ( int i = 0; i < nSize; ++i) {out[i] = in[nSize - i - 1];}

    out[nSize + 1] = '\0';
}

------------------------------------------->8----------------------------------

and/or

------------------------------------------->8----------------------------------

OK, that looks better - now, have you taken a look at what I suggested? When you reverse a string and it matches the original (hint: 'strcmp()') you found a palindrome...

------------------------------------------->8----------------------------------

?
However, that remains unheard for some strange reason...
sorry paul..i didnt know that..anyway my idea would be is

1)get the i/p in string1and store the same i/p in another string..say string2
hint:u can use a pointer to do this!
2)and now compare the first value in the first string1  and the last variable in the
string2..

   these are the two main hints..then u can easily tell whether its a palindrome or not..
>>Kelvin_King,

>>I'd rather just construct the string in reverse and then compare the 2 strings.

>>What do you think I was referring to with

sorry didnt see it.
Thanks mbrinks3 - that kind of responsiveness is really what keeps me on this site and motivates people :-(
You talking to me ? I didn't post the question.
Hello Im new around here and watched this question and wanted to provide a solution took me a little more than 5 minutes but then i saw it was already solved, is it? Anyways I would love to share my view on it...

<Code removed for reasons already clear by PE. Sorry!>

Compiled on Dev-Cpp...

I hope Helps someone a little ;)

I always say there are infinite ways to reach the solution of a problem, and these solutions may help to resolve not only this, but N numbers of problems
Hi Ryoshin,

Welcome to EE ... We love to have problem solvers around here ... However, in this particular question there is a glitch.
This is a student's course assignment and as a matter of policy, we do not provide coded solutions to students. We just try to guide them and encourage them to reach the solution on their own. That way, they get more than just a single solution.

I see that this is your first post. Please take some time to read the help pages and membership agreements(MA). All these guidelines are listed there and would help you use the site better and gel into the community.
https://www.experts-exchange.com/memberAgreement.jsp
https://www.experts-exchange.com/help.jsp

Hope you enjoy your time at EE. :)

Cheers!
sunnycoder

PS: Dont be surprised if your code gets edited out. Paul (our Page Editor) is subscribed to the thread. He manages the C Programming Channel and tries to ensure that there are no MA voilations
Ok, cool...no problem there. ^^