Thread: Reverse string and compare

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    1

    Reverse string and compare

    Hi!

    Having some problems with functions in this one. As ypu can see I'm a tottal beginner. What I'm trying to do here is make a program checking if a word is a palindrome. Using 2 functions, one to check and one to reverse the user input string. My problem is simple, yet to hard for me. I can't seem to get to grips with the use of functions and how to call them.
    The code I've written so far looks like this. Any help or pointers would be very appreciated.

    insert
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <string.h>
    
    bool palindromeCheck (char str1[], char str2[])
    {
         int i=0;
         bool equal;
            
         while (str1[i] == str2[i] && str1[i] != '\0' && str2[i] != '\0')
         i++;
         
         if (str1[i] == '\0' && str2[i] =='\0')
            equal = true;
         else
             equal = false;
             
         return equal;
         }
         
    char strReverse(char str1[], char str2[])
    {
          int i=0, j=0;
          while(str1 != '\0')
          j++;
          j--;
          while(i<=j)
          {
          char t = str1[i];
          str1[i++] = str2[j];
          str2[j--] = t;
          }
          
          return str2[100];
    }     
    
    int main (void)
    {
        bool palindromeCheck (char str1[]);
        char strReverse;
        char str1[100];    
        char str2[100];
        int i;
        bool equal;    
        printf ("Enter word:");
        scanf (" %s", str1);
        
        strcpy (str1, str2);
         
        printf ("\n\n");
            
        if (equal=true)
        {
           printf ("Word is a plaindrome!\n");
           }
        else
        {
            printf ("Word is not a palindrome!\n");
            }
    
        system ("pause");
        return 0;
        
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    if (equal=true)
    That *assigns* the value true to equal, it does not *compare* the two.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You also need to call your "reverse" and "check" functions as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 05-31-2010, 10:57 AM
  2. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. String Compare Using Dynamic Arrays
    By djwicks in forum C Programming
    Replies: 4
    Last Post: 03-31-2005, 08:01 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM